C # Network application programming basic exercises and Answers (ix)

Source: Internet
Author: User
Programming | network

1. Briefly describe the similarities and differences between the three types of two-dimensional coordinate systems.

Troubleshooting

The two-dimensional Cartesian coordinate system in GDI + is divided into three categories: global coordinate system, page coordinate system and equipment coordinate system.

The same point of three types of coordinate systems: they all have coordinate origin and the x and Y axes of right and down.

The difference of three kinds of coordinate systems: the global coordinate system can rotate, translate and so on. page coordinate system and equipment coordinate system are in the upper-left corner of the device as the coordinate origin, X level to the right, y vertical downward as positive. The difference between the page coordinate system and the equipment coordinate system is that the units of the x,y are different: the X,y units in the page coordinates can be set arbitrarily, such as inches, millimeters, and so on, and in the device coordinates, there is only one unit, that is point or pixel (pixel). The page coordinate system cannot be changed, it is a reference standard, and the global coordinates are eventually converted to device coordinates.

2. Briefly describe the three methods of creating graphics class objects.

Troubleshooting

(1) Reference the Graphics object directly in the paint event of the form or control. You typically use this method to get a reference to an image when you create a drawing code for the form.

(2) Gets a reference to the Graphics object from the current form. Note This object is valid only if you are working with the current Windows Forms message. You can use this method if you want to draw in a form or control that already exists.

(3) Create a Graphics object from any object inherited from the image. This method applies to the need to change the existing

Image.

3. Create multiple rectangles and fill them with red and black colors.

Troubleshooting

(1) Create a new Windows application named "Fillmultirectangle" and adjust the form to the appropriate size. Change "Form1.cs" to "FormFillMultiRectangle.cs".

(2) switch to code to add a namespace reference:

Using System.Drawing.Drawing2D;

(3) Double-click the Paint event in the event page of the form's property panel to add the Formfillmultirectangle_paint event code.

private void Formfillmultiretangle_paint (object sender, PaintEventArgs e)
{
Graphics g = e.graphics;
HatchBrush mybrush=new HatchBrush (hatchstyle.cross,color. Red,color. Black);
Rectangle[] Rect=
{
New Rectangle (0, 0, 50, 100),
New Rectangle (50,100, 100, 50),
New Rectangle (150, 150, 50, 100),
};
G.fillrectangles (MyBrush, rect);
G.dispose ();
}

(4) The result is shown in the figure.

4. Use the Label control to display the picture in rectangles, ellipses, and circles, respectively, and to ensure that the picture is entirely determined by the bounding rectangle of the drawing object.

Troubleshooting

(1) Create a new Windows application named "Showimageexe" and adjust the form to the appropriate size. Change "Form1.cs" to "FromShowImageExe.cs".

(2) switch to code to add a namespace reference:

Using System.Drawing.Drawing2D;

(3) Add four button controls named "Buttonopenfile", "Buttonrectangle", "Buttonellipse", "Buttonround" respectively, and a Openfilediolog and label control.

(4) Declare two private variables filename and flag under the form class, respectively to record the open file name and to determine which button's click Time is triggered.

private string filename = "";

private int flag = 0;

(5) Add the Click event of the "Open File" button

private void Buttonopenfile_click (object sender, EventArgs e)
{
Openfiledialog1.showdialog ();
filename = openfiledialog1.filename;
Label1. Refresh ();
}

(6) Add the following code under the Paint event for the Label1 control:

private void Label1_paint (object sender, PaintEventArgs e)
{
if (filename. Trim () = = "")
Return
Bitmap Mybitmap = new Bitmap (filename);
Graphics g = e.graphics;
TextureBrush MyBrush = new TextureBrush (mybitmap,wrapmode.clamp);
Make sure the picture is completely determined by the border of the drawing object
Switch (flag)
{
Case 1:
G.fillrectangle (MyBrush, Label1. ClientRectangle);
Break
Case 2:
G.fillellipse (MyBrush, Label1. ClientRectangle);
Break
Case 3:
G.fillellipse (MyBrush, Label1. Width-label1. Height)/2,0, Label1. Height, Label1. Height);
Break
}
}

(7) Add the following code to the click event of several other buttons:

private void Buttonrectangle_click (object sender, EventArgs e)
{
flag = 1;
Label1. Refresh ();
}
private void Buttonellipse_click (object sender, EventArgs e)
{
flag = 2;
Label1. Refresh ();
}
private void Buttonround_click (object sender, EventArgs e)
{
flag = 3;
Label1. Refresh ();
}

  

(8) The result is shown in the figure.

5. Use the PictureBox control and the Panel control implementation to navigate through the large picture using the scroll bar.

Troubleshooting

Because the PictureBox control cannot use the scroll bar directly when it displays a picture, the Panel control must be used to implement a scroll bar to view large pictures. The specific steps are as follows:

(1) Create a new Windows application named "ScrollBar" and adjust the form to the appropriate size. Change "Form1.cs" to "FormScrollBar.cs".

(2) switch to code to add a namespace reference:

Using System.Drawing.Drawing2D;

(3) Add a button control on the form named "Buttonopenfile", a Openfilediolog control, PictureBox and a panel control, set the Panel control's AutoScroll property to True.

(4) Add the following code to the click event of the "Buttonopenfile" control:

private void Buttonopenfile_click (object sender, EventArgs e)
{
Openfiledialog1.showdialog ();
if (OpenFileDialog1.FileName.Trim () = "")
Return
Try
{
Bitmap Mybitmap = new Bitmap (openfiledialog1.filename);
pictureBox1.Image = Mybitmap;
}
catch (Exception ERR)
{
MessageBox.Show ("Open File Error!", "Information hint", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

(5) The result is shown in the figure.

  

6. The realization of the picture to rotate at any angle, in any proportion of the scaling, according to any displacement translation.

Troubleshooting

(1) Add six label controls on the form (where Label1 is used to display pictures), a button control (for opening picture files), and five NumericUpDown controls (used to select the proportions of the picture scaling, the angle of the picture rotation, the size of the picture's displacement, respectively).

(2) Add code above the constructor:

private string Strfilename= "";

(3) Add the following code in the Click event of the button control:

private void Button1_Click (object sender, EventArgs e)
{
Openfiledialog1.showdialog ();
Strfilename=openfiledialog1.filename;
Label1. Refresh ();
}

(4) Add the following code to the ValueChanged event for each NumericUpDown control:

Label1. Refresh ();

(5) Add the following code to the Paint event of the Label1 control:

private void Label1_paint (object sender, PaintEventArgs e)
{
if (This.strfilename.Trim () = "")
return;
Try
{
Bitmap Mybitmap = new Bitmap (strFileName);
Graphics g = e.graphics;
TextureBrush MyBrush = new TextureBrush (MYBITMAP);
float x = (float) (numericupdowns1.value/100);
Float y = (float) (numericupdowns2.value/100);
MyBrush. ScaleTransform (x, y);
G.fillrectangle (mybrush, 0, 0, clientrectangle.width, clientrectangle.height);
float R = (float) (numericupdownr1.value);
MyBrush. RotateTransform (R);
G.fillrectangle (mybrush, 0, 0, clientrectangle.width, clientrectangle.height);
float tx = (float) (numericupdownt1.value);
float Ty = (float) (numericupdownt2.value);
MyBrush. TranslateTransform (TX, Ty);
G.fillrectangle (mybrush, 0, 0, clientrectangle.width, clientrectangle.height);
}
catch (Exception ERR)
{
MessageBox.Show ("Open File Error!", "Information hint", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

8-8-6



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.