Vb Draw Box Around Group of Tables

The Framework provides several drawing methods, one for each basic shape. Yous can create much more elaborate shapes by combining the methods described in the following sections.

All drawing methods take a few things in common. The beginning statement is always a Pen object, which will be used to return the shape on the Graphics object. The following arguments are the parameters of a shape: They make up one's mind the location and dimensions of the shape. The DrawLine method, for example, needs to know the endpoints of the line to draw, whereas the DrawRectangle method needs to know the origin and dimensions of the rectangle to depict. The parameters needed to render the shape are passed as arguments to each drawing method, post-obit the Pen object.

The drawing methods tin can besides be categorized in two major groups: the methods that describe stroked shapes (outlines) and the methods that draw filled shapes. The methods in the first group commencement with the Depict prefix (DrawRectangle, DrawEllipse, and and so on). The methods of the second group start with the Fill prefix (FillRectangle, FillEllipse, and then on). Of course, some DrawXXX methods don't have an equivalent FillXXX method. For example, you lot tin can't fill a line or an open up curve, so there are no FillLine or FillCurve methods.

Another departure betwixt the drawing and filling methods is that the filling methods use a Brush object to fill the shape — you can't make full a shape with a pen. Then, the offset argument of the methods that depict filled shapes is a Brush object, non a Pen object. The remaining arguments are the same because yous must however specify the shape to be filled. In the following sections, I present in particular the shape-cartoon methods merely not the shape-filling methods. If you tin can use a drawing method, you lot can just every bit easily utilize its filling counterpart.

Table 14.4 shows the names of the cartoon methods. The starting time column contains the methods for drawing stroked shapes, and the second column contains the corresponding methods for drawing filled shapes (if at that place'south a matching method).

Table 14.4 – The Cartoon Methods

Drawing Method Filling Method Description
DrawArc Draws an arc
DrawBezier Draws very shine curves with fixed endpoints, whose exact shape is determined by ii command points
DrawBeziers Draws multiple Bezier curves in a single call
DrawClosedCurve FillClosedCurve Draws a closed curve
DrawCurve Draws curves that pass through certain points
DrawEllipse FillEllipse Draws an ellipse
DrawIcon Renders an icon on the Graphics object
DrawImage Renders an image on the Graphics object
DrawLine Draws a line segment
DrawLines Draws multiple line segments in a single call
DrawPath FillPath Draws a GraphicsPath object
DrawPie FillPie Draws a pie department
DrawPolygon FillPolygon Draws a polygon (a series of line segments between points)
DrawRectangle FillRectangle Draws a rectangle
DrawRectangles FillRectangles Draws multiple rectangles in a single call
DrawString Draws a string in the specified font on the drawing surface
FillRegion Fills a Region object

Some of the drawing methods allow you lot to draw multiple shapes of the aforementioned type, and they're properly named DrawLines, DrawRectangles, and DrawBeziers. We simply supply more shapes as arguments, and they're drawn one after the other with a single call to the corresponding method. The multiple shapes are stored in arrays of the aforementioned type as the individual shapes. The DrawRectangle method, for example, accepts as an argument the Rectangle object to be drawn. The DrawRectangles method accepts equally an argument an array of Rectangle objects and draws all of them at once.

DrawLine

The DrawLine method draws a straight-line segment between two points with a pen supplied equally an statement. The simplest forms of the DrawLine method are the following, where point1 and point2 are either Point or PointF objects, depending on the coordinate system in utilize:

          

Graphics .DrawLine(pen, X1, Y1, X2, Y2) Graphics .DrawLine(pen, point1, point2)

Code language: CSS ( css )

DrawRectangle

The DrawRectangle method draws a stroked rectangle and has two forms:

          

Graphics .DrawRectangle(pen, rectangle) Graphics .DrawRectangle(pen, X1, Y1, width, height)

Lawmaking linguistic communication: CSS ( css )

The rectangle statement is a Rectangle object that specifies the shape to be drawn. In the 2nd grade of the method, the arguments X1 and Y1 are the coordinates of the rectangle'southward superlative-left corner, and the other two arguments are the dimensions of the rectangle. All these arguments can exist integers or singles, depending on the coordinate system in use. However, they must be all of the same type.

The following statements describe two rectangles, i inside the other. The outer rectangle is fatigued with a red pen with the default width, whereas the inner rectangle is drawn with a 3-pixel-wide green pen and is centered inside the outer rectangle:

          

Yard .DrawRectangle(Pens .Red, 100, 100, 200, 100) Yard .DrawRectangle(New Pen(Color .Green, 3), 125, 125, 150, 50)

Code language: CSS ( css )

DrawEllipse

An ellipse is an oval or circular shape, determined by the rectangle that encloses it. The 2 dimensions of this rectangle are the ellipse'south major and minor diameters. Instead of giving y'all a mathematically correct definition of an ellipse, I prepared a few ellipses with unlike ratios of their 2 diameters (these ellipses are shown in Figure 14.seven). The figure was prepared with the GDIPlus sample application (download here), which demonstrates a few more graphics operations. The ellipse is oblong along the direction of the major bore and squashed along the direction of the pocket-sized diameter. If the two diameters are exactly equal, the ellipse becomes a circle. Indeed, the circle is just a special case of the ellipse, and in that location's no DrawCircle method.

To draw an ellipse, call the DrawEllipse method, which has ii bones forms:

          

Graphics .DrawEllipse(pen, rectangle) Graphics .DrawEllipse(pen, X1, Y1, width, peak)

Code linguistic communication: CSS ( css )

The arguments are the same as with the DrawRectangle method because an ellipse is basically a circumvolve deformed to fit in a rectangle. The 2 ellipses and their enclosing rectangles shown in Figure 14.7 were generated with the statements of List 14.5.

Two ellipses with their enclosing rectangles
Effigy 14.7 – Two ellipses with their enclosing rectangles

Listing xiv.5: Drawing Ellipses and Their Enclosing Rectangles

          

Private Sub bttnEllipses_Click(ByVal sender As Organisation.Object, _ ByVal east As System.EventArgs) Handles bttnEllipses.Click Dim Yard As Graphics K = PictureBox1.CreateGraphics Thou.Clear(PictureBox1.BackColor) Thou.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias Thousand.FillRectangle(Brushes.Silver, ClientRectangle) Dim R1, R2 Every bit Rectangle R1 = New Rectangle(10, ten, 160, 320) R2 = New Rectangle(200, 85, 320, 160) Chiliad.DrawEllipse(New Pen(Color.Blackness, 3), R1) Chiliad.DrawRectangle(Pens.Black, R1) G.DrawEllipse(New Pen(Color.Blackness, iii), R2) K.DrawRectangle(Pens.Red, R2) End Sub

Code language: PHP ( php )

The ellipses were drawn with a 3-pixel-broad pen. Every bit you tin see in the effigy, the width of the ellipse is split to the within and outside of the enclosing rectangle, which is drawn with a 1-pixel-wide pen (download source lawmaking).

DrawPie

A pie is a shape similar to a slice of pie (an arc along with the 2 line segments that connect its endpoints to the eye of the circle or the ellipse, to which the arc belongs). The DrawPie method accepts every bit arguments the pen with which it will depict the shape, the circle to which the pie belongs, the arc's starting angle, and its sweep bending. The circle (or the ellipse) of the pie is defined with a rectangle. The starting and sweeping angles are measured clockwise. The DrawPie method has ii forms:

          

Graphics .DrawPie(pen, rectangle, start, sweep) Graphics .DrawPie(pen, Ten, Y, width, height, offset, sweep)

Code language: CSS ( css )

The 2 forms of the method differ in how the rectangle is defined (a Rectangle object versus its coordinates and dimensions). The get-go statement is the pie's starting bending, and sweep is the angle of the pie. The ending angle is kickoff + sweep. Angles are measured in degrees (at that place are 360 degrees in a circle) and increment in a clockwise management. The 0 angle corresponds to the horizontal axis.

The statements of List fourteen.6 create a pie nautical chart by drawing individual pie slices. Each pie starts where the previous one ends, and the sweeping angles of all pies add up to 360 degrees, which corresponds to a full rotation (a full circle). Dissimilar the other samples of this section, I've used the FillPie method, because we inappreciably ever draw the outlines of the pies; nosotros fill up each one with a different colour instead. Effigy 14.8 shows the output produced by Listing 14.half-dozen.

A simple pie chart generated with the FillPie method
Figure xiv.viii – A uncomplicated pie chart generated with the FillPie method

Listing xiv.6: Cartoon a Simple Pie Chart with the FillPie Methods

          

Individual Sub bttnPie_Click(ByVal sender As Organisation.Object, _ ByVal e Every bit System.EventArgs) Handles Button2.Click Dim m As Arrangement.Drawing.Graphics thou = Me.CreateGraphics Dim castor As System.Drawing.SolidBrush Dim rect As Rectangle castor = New Organisation.Drawing.SolidBrush(Color.Green) Dim Angles() Equally Unmarried = {0, 43, 79, 124, 169, 252, 331, 360} Dim colors() Equally Color = {Color.Red, Color.Cornsilk, _ Color.Firebrick, Colour.OliveDrab, Colour.LawnGreen, _ Colour.SandyBrown, Color.MidnightBlue} thou.Clear(Colour.Ivory) rect = New Rectangle(100, ten, 300, 300) Dim angle As Integer For angle = one To Angles.GetUpperBound(0) castor.Color = colors(angle - ane) g.FillPie(brush, rect, Angles(angle - 1), _ Angles(angle) - Angles(angle - 1)) Next m.DrawEllipse(Pens.Black, rect) End Sub

Code language: PHP ( php )

The code sets upward two arrays: ane with angles and another with colors. The Angles array holds the starting angle of each pie. The sweep angle of each pie is the difference betwixt its own starting angle and the starting angle of the post-obit pie. The sweep angle of the start pie is Angles(one) Angles(0), which is 43 degrees. The loop goes through each pie and draws it with a color information technology picks from the Colors array, based on the angles stored in the Angles array. In your awarding, yous must calculate the full of a quantity (such as all customers, or all units of a product sold in a territory) then use the individual percentages to set up the starting and catastrophe angles of each pie. If there are 800 customers and 20 of them belong to a specific expanse, this expanse'due south sweep angle should exist 1/xl of the circumvolve, which is nine degrees.

Observe that the FillPie method doesn't connect the pie'southward endpoints to the eye of the ellipse. The 2d button on the PieChart project's form draws the aforementioned pie chart, but it also connects each slice'south endpoints to the center of the circle. The code behind this button is identical to the code shown in List fourteen.6 — with the exception that after calling the FillPie method, information technology calls the DrawPie method to draw the outline of the pie. (download source code)

DrawPolygon

The DrawPolygon method draws an arbitrary polygon. It accepts 2 arguments: the Pen that information technology will use to return the polygon and an array of points that define the polygon. The polygon has as many sides (or vertices) every bit there are points in the array, and information technology's ever closed, fifty-fifty if the first and concluding points are non identical. In fact, you practice non demand to repeat the starting indicate at the terminate because the polygon will be automatically closed. The syntax of the DrawPolygon method is the following:

          

Graphics .DrawPolygon(pen, points())

Lawmaking language: CSS ( css )

where points is an array of points, which can be declared with a statement like the following:

          

Dim points() As Point = {New Point(x1, y1), New Betoken(x2, y2), ...}

Code language: PHP ( php )

DrawCurve

Curves are smooth lines drawn as cardinal splines. A real spline is a flexible object (made of soft wood) that designers used to flex on the cartoon surface with spikes. The spline goes through all the fixed points and assumes the smoothest possible shape, given the restrictions imposed by the spikes. If the spline isn't flexible enough, information technology breaks. In modern figurer graphics, in that location are mathematical formulas that depict the path of the spline through the stock-still points and accept into consideration the tension (the degree of flexibility) of the spline. A more than flexible spline yields a curve that bends hands. Less-flexible splines practise not curve hands around their fixed points. Estimator-generated splines practise not break, but they can take unexpected shapes.

To draw a bend with the DrawCurve method, you specify the locations of the spikes (the points that the spline must become through) and the spline'southward tension. If the tension is 0, the spline is totally flexible, like a prophylactic band: All the segments between points are straight lines. The higher the tension, the smoother the curve will be. Figure fourteen.viii shows 4 curves passing through the same points, but each curve is fatigued with a different tension value. The curves shown in the effigy were drawn with the GDIPlus project (using the Ordinal Curves push button).

The simplest grade of the DrawCurve method has the following syntax, where points is an array of points:

          

Graphics .DrawCurve(pen, points, tension)

Code language: CSS ( css )

The beginning and last elements of the assortment are the curve's endpoints, and the curve volition become through the remaining points likewise.

The curves shown in Figure xiv.9 were produced past the lawmaking shown in Listing xiv.seven. Notice that a tension of 0.five is practically the same as 0 (the spline bends around the fixed points like a rubber band). If you lot drew the same curve with a tension of 5, you'd get an odd curve indeed because although a physical spline would break, the mathematical spline takes an unusual shape to accommodate the stock-still points. (download source code)

These curves go through the same points, but they have different tensions.
Effigy 14.nine – These curves go through the same points, but they have different tensions.

Listing 14.vii: Curves with Mutual Fixed Points and Unlike Tensions

          

Private Sub bttnCurves_Click(ByVal sender As Organization.Object, _ ByVal e As System.EventArgs) Handles bttnCurves.Click Dim G As Graphics G = PictureBox1.CreateGraphics M.Clear(PictureBox1.BackColor) Thousand.FillRectangle(Brushes.Silver, ClientRectangle) G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality Dim points() As Indicate = {New Indicate(20, 50), New Point(220, 190), _ New Point(330, eighty), New Indicate(450, 280)} Thousand.DrawCurve(Pens.Blue, points, 0.1) G.DrawCurve(Pens.Red, points, 0.five) G.DrawCurve(Pens.Greenish, points, 1) Thou.DrawCurve(Pens.Black, points, ii) End Sub

Lawmaking language: PHP ( php )

DrawBezier

The DrawBezier method draws Bezier curves, which are smoother than central splines. A Bezier curve is defined by two endpoints and two control points. The control points act equally magnets. The bend is the trace of a point that starts at one of the endpoints and moves toward the second one. Every bit it moves, the point is attracted by the two control points. Initially, the first control betoken's influence is predominant. Gradually, the bend comes into the 2nd command betoken's field and it ends at the 2nd endpoint.

The DrawBezier method accepts a pen and four points as arguments:

          

Graphics .DrawBexier(pen, X1, Y1, X2, Y2, X3, Y3, X4, Y4) Graphics .DrawBezier(pen, point1, point2, point3, point4)

Lawmaking language: CSS ( css )

Figure 14.10 shows four Bezier curves, which differ in the y-coordinate of the third control indicate. All control points are marked with fiddling squares: one each for the three points that are common to all curves, and four in a vertical column for the point that differs in each bend.

Bezier curves and their control points
Figure 14.10 – Bezier curves and their control points

The code of Listing xiv.viii draws the four Bezier curves (I'yard not showing the statements that draw the small rectangles; they simply call the FillRectangle method). The endpoints and one control point (P1, P2, and P4) remain the same, whereas the other command signal (P3) is ready to iv unlike values. Notice how far the control indicate must go to have a pregnant effect on the curve's shape.

Listing xiv.8: Drawing Bezier Curves and Their Control Points

          

Private Sub bttnBezier_Click(ByVal sender Every bit System.Object, _ ByVal e As Arrangement.EventArgs) Handles bttnBezier.Click Dim Yard As Graphics 1000 = PictureBox1.CreateGraphics M.Clear(PictureBox1.BackColor) Thousand.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias G.FillRectangle(Brushes.Silver, ClientRectangle) Dim P1 As New Point(120, 150) Dim P2 Equally New Point(220, ninety) Dim P3 Every bit New Signal(330, xxx) Dim P4 As New Point(410, 110) Dim sqrSize As New Size(6, six) Yard.FillRectangle(Brushes.Black, New Rectangle(P1, sqrSize)) Thou.FillRectangle(Brushes.Blackness, New Rectangle(P2, sqrSize)) M.FillRectangle(Brushes.Cherry-red, New Rectangle(P3, sqrSize)) M.FillRectangle(Brushes.Black, New Rectangle(P4, sqrSize)) G.DrawBezier(Pens.Blue, P1, P2, P3, P4) P3 = New Indicate(330, 130) Thou.FillRectangle(Brushes.Scarlet, New Rectangle(P3, sqrSize)) K.DrawBezier(Pens.Blueish, P1, P2, P3, P4) P3 = New Indicate(330, 230) M.FillRectangle(Brushes.Red, New Rectangle(P3, sqrSize)) G.DrawBezier(Pens.Blue, P1, P2, P3, P4) P3 = New Indicate(330, 330) Thou.FillRectangle(Brushes.Red, New Rectangle(P3, sqrSize)) Grand.DrawBezier(Pens.Blue, P1, P2, P3, P4) End Sub

Code linguistic communication: PHP ( php )

To draw the curve, all you demand is to specify the four control points and pass them forth with a Pen object to the DrawBezier method. (download source code)

DrawPath

This method accepts a Pen object and a Path object as arguments and renders the specified path on the screen:

          

Graphics .DrawPath(pen, path)

Code linguistic communication: CSS ( css )

To construct the Path object, use the AddXXX methods (AddLine, AddRectangle, and then on) refer to the sub topic called "The Path Grade," earlier discussed in the department "Basic Drawing Objects". You will find an example of how to apply the Path object later in this chapter, when yous'll learn how to plot functions.

DrawString, MeasureString

The DrawString method renders a string in a single line or multiple lines. Equally a reminder, the TextRenderingHint belongings of the Graphics object allows yous to specify the quality of the rendered text. The simplest form of the DrawString method is the following:

          

Graphics .DrawString(string, font, brush, X, Y)

Code language: CSS ( css )

The commencement argument is the string to be rendered in the font specified by the second statement. The text will be rendered with the Brush object specified by the brush statement. X and Y, finally, are the coordinates of the peak-left corner of a rectangle that completely encloses the string. While working with strings, in most cases you demand to know the bodily dimensions of the string when rendered with the DrawString method in the specified font. The MeasureString method allows y'all to think the metrics of a string before actually drawing it. This method returns a SizeF construction with the width and height of the string when rendered on the same Graphics object with the specified font. We'll employ this method extensively in Chapter, "Printing with Visual Basic 2008," to position text precisely on the printed page. You can also pass a Rectangle object as an argument to the MeasureString method to detect out how many lines it volition take to render the string on the rectangle.

The simplest form of the MeasureString method is the post-obit, where string is the string to be rendered and font is the font in which the string volition be rendered:

          

Dim textSize As SizeF textSize = Me.Graphics.MeasureString(cord, font)

Code language: PHP ( php )

To center a cord on the form, utilize the x-coordinate returned by the MeasureString method, as in the following code segment:

          

Dim textSize As SizeF Dim Ten As Integer, Y As Integer = 0 textSize = Me.Graphics.MeasureString(string, font) 10 = (Me.Width - textSize.Width) / 2 G.DrawString("Centered string", font, brush, X, Y)

Code linguistic communication: PHP ( php )

Nosotros subtract the rendered string'south length from the form's width, and we dissever the difference in half at the two sides of the string.

Effigy xiv.11 shows a cord printed at the center of the form and the two lines passing through the same bespeak. List 14.9 shows the statements that produced the string. This list is function of the TextEffects sample project.

Centering a string on a form
Figure 14.11 – Centering a string on a form

Listing fourteen.9: Press a Cord Centered on the Form

          

Private Sub center(ByVal sender As System.Object, _ ByVal due east Every bit Organization.EventArgs) Handles bttnCentered.Click Dim Grand As Graphics G = GetGraphicsObject() G.FillRectangle(New SolidBrush(Color.Silver), ClientRectangle) Chiliad.TextRenderingHint = Cartoon.Text.TextRenderingHint.AntiAlias FontDialog1.Font = Me.Font FontDialog1.ShowDialog() Dim txtFont As Font txtFont = FontDialog1.Font G.DrawLine(New Pen(Color.Dark-green), CInt(Me.Width / ii), _ CInt(0), CInt(Me.Width / ii), CInt(Me.Height)) G.DrawLine(New Pen(Colour.Green), 0, CInt(Me.Height / 2), _ CInt(Me.Width), CInt(Me.Superlative / ii)) Dim drawString As String = "Visual Basic 2008" Dim txtSize As SizeF txtSize = G.MeasureString(drawString, txtFont) Dim txtX, txtY Every bit Integer txtX = (Me.Width - txtSize.Width) / 2 txtY = (Me.Peak - txtSize.Height) / two G.DrawString(drawString, txtFont, New SolidBrush(Color.Red), txtX, txtY) Dim boxSize As SizeF boxSize = G.MeasureString(drawString, txtFont) G.DrawRectangle(Pens.Black, txtX, txtY, boxSize.Width, boxSize.Tiptop) Me.Invalidate() End Sub

Code language: PHP ( php )

The coordinates passed to the DrawString method (variables txtX and txtY) are the coordinates of the tiptop-left corner of the rectangle that encloses the first graphic symbol of the string. Another class of the DrawString method accepts a rectangle as an argument and draws the string in this rectangle, breaking the text into multiple lines if needed. The syntax of this form of the method is every bit follows:

          

Graphics .DrawString(string, font, brush, rectanglef) Graphics .DrawString(cord, font, brush, rectanglef, stringFormat)

Lawmaking language: CSS ( css )

If you desire to render text in a box, you lot will most likely use the equivalent form of the MeasureString method to recall the metrics of the text in the rectangle. This class of the MeasureString method returns the number of lines it will take to render the string in the supplied rectangle, and it has the following syntax, where string is the text to be rendered, and font is the font in which the cord volition be rendered:

          

e .Graphics .MeasureString(string, font, fitSize, stringFormat, lines, cols)

Code language: CSS ( css )

The fitSize statement is a SizeF object that represents the width and height of a rectangle, where the string must fit. The lines and cols variables are passed past reference, and they are set by the MeasureString method to the number of lines and number of characters that will fit in the specified rectangle. The exact location of the rectangle doesn't brand whatsoever difference — only its dimensions matter, and that'south why the third argument is a SizeF object, not a Rectangle object. Figure 14.12 shows a cord printed in two unlike rectangles past the TextEffects sample project; the figure was created with the Draw Boxed Text button. The code that produced the effigy is shown in Listing 14.10.

Printing Text in a Rectangle
Figure 14.12 – Printing Text in a Rectangle

Listing 14.10: Printing Text in a Rectangle

          

Private Sub BoxedText(ByVal sender As System.Object, _ ByVal eastward As System.EventArgs) Handles bttnBoxed.Click Dim G Every bit Graphics G = GetGraphicsObject() Yard.FillRectangle(New SolidBrush(Color.Silvery), ClientRectangle) FontDialog1.Font = Me.Font FontDialog1.ShowDialog() Dim txtFont As Font txtFont = FontDialog1.Font Dim txt Every bit Cord = "This text was rendered in a rectangle with the " & _ "DrawString method of the Form's Graphics object. " txt = txt & txt & txt & txt & txt G.DrawString(txt, txtFont, Brushes.Black, New RectangleF(100, eighty, 180, 250)) G.DrawRectangle(Pens.Red, 100, 80, 180, 250) 1000.DrawString(txt, txtFont, Brushes.Black, New RectangleF(350, 100, 400, 150)) M.DrawRectangle(Pens.Ruby-red, 350, 100, 400, 150) Me.Invalidate() End Sub

Code language: PHP ( php )

The StringFormat Object

Some of the overloaded forms of the DrawString method accept an argument of the StringFormat type. This argument determines characteristics of the text and exposes a few properties of its own, which include the following:

  • Alignment – Determines the alignment of the text; its value is a member of the StringAlignment enumeration: Eye (text is aligned in the center of the layout rectangle), Far (text is aligned far from the origin of the layout rectangle), and Almost (text is aligned near the origin of the layout rectangle).
  • Trimming – Determines how text will be trimmed if information technology doesn't fit in the layout rectangle. Its value is one of the members of the StringTrimming enumeration: Character (text is trimmed to the nearest character), EllipsisCharacter (text is trimmed to the nearest grapheme and an ellipsis is inserted at the end to indicate that some of the text is missing), EllipsisPath (text at the middle of the cord is removed and replaced by an ellipsis), EllipsisWord (text is trimmed to the nearest discussion and an ellipsis is inserted at the cease), None (no trimming), and Word (text is trimmed to the nearest give-and-take).
  • FormatFlags – Specifies layout information for the cord. Its value can exist ane of the members of the StringFormatFlags enumeration. The two members of this enumeration that you might need often are DirectionRightToLeft (prints to the left of the specified indicate) and DirectionVertical.

To utilize the stringFormat argument of the DrawString method, instantiate a variable of this type, ready the desired properties, and so pass it every bit an argument to the DrawString method, every bit shown here:

          

Dim G As Graphics = Me.CreateGraphics Dim SF As New StringFormat() SF.FormatFlags = StringFormatFlags.DirectionVertical Thou.DrawString("Visual Bones", Me.Font, Brushes.Red, 80, lxxx, SF)

Code language: PHP ( php )

The call to the DrawString method will print the string from height to bottom. Information technology will likewise rotate the characters. The DirectionRightToLeft setting will cause the DrawString method to print the string to the left of the specified point, merely information technology volition not mirror the characters.

You can discover additional examples of the MeasureString method in Chapter "Printing with Visual Basic", in which we'll use this method to fit strings on the width of the folio. The third button on the class of the TextEffects projection draws text with a 3-dimensional look by overlaying a semitransparent cord over an opaque string. This technique is explained in the "Alpha Blending" section in Chapter "Manipulating Images and Bitmaps", in which you'll acquire how to use transparency. You lot might also wonder why none of the DrawString methods' forms take as an argument an angle of rotation for the text. You tin draw text or any shape at any orientation as long as you set the proper rotation transformation. This topic is discussed in the "Applying Transformations" section afterwards in this chapter, also as in Chapter "Printing with Visual Basic".

DrawImage

The DrawImage method, which renders an epitome on the Graphics object, is a heavily overloaded and quite flexiblemethod. The following form of themethod draws the paradigm at the specified location. Both the image and the location of its top-left corner are passed to the method as arguments (as Paradigm and Point arguments, respectively):

          

Graphics .DrawImage(img, point)

Code language: CSS ( css )

Another class of the method draws the specified image within a rectangle. If the rectangle doesn't match the original dimensions of the paradigm, the paradigm will be stretched to fit in the rectangle. The rectangle should have the aforementioned aspect ratio as the Epitome object, to avoid distorting the image in the process.

          

Graphics .DrawImage(img, rectangle)

Code language: CSS ( css )

Some other course of the method allows you to change non just the magnification of the epitome, just as well its shape. This method accepts as an argument non a rectangle, only an array of three points that specifies a parallelogram. The paradigm will exist sheared to fit in the parallelogram, where points is an array of points that ascertain a parallelogram:

          

Graphics .DrawImage(img, points())

Lawmaking linguistic communication: CSS ( css )

The assortment holds three points, which are the top-left, top-right, and bottom-left corners of the parallelogram. The quaternary point is determined uniquely by the other 3, and you lot need non supply information technology. The ImageCube sample project, shown later on in this affiliate, uses this overloaded grade of the DrawImage method to describe a cube with a different epitome on each face. Another interesting form of the method allows y'all to set the attributes of the image:

          

Graphics .DrawImage(image, points(), srcRect, units, attributes)

Code linguistic communication: CSS ( css )

The start two arguments are the same every bit in the previous forms of the method. The srcRect statement is a rectangle that specifies the portion of image to draw, and units is a constant of the GraphicsUnit enumeration. Information technology determines how the units of the rectangle are measured (pixels, inches, and so on). The last statement is an ImageAttributes object that contains information well-nigh the attributes of the epitome you lot want to change (such every bit the gamma value, and a transparent colour value or colour key). The properties of the ImageAttributes course are discussed soon. The DrawImage method is quite flexible, and you can use it for many special effects, including wipes. A wipe is the gradual advent of an image on a form or PictureBox control. Y'all tin can utilize this method to draw stripes of the original image, or start with a small rectangle in the middle that grows gradually until information technology covers the entire paradigm.

You can also correct the color of the image past specifying the attributes statement. To specify the attributes argument, create an ImageAttributes object with a argument like the following:

          

Dim attr Equally New Organisation .Drawing .Imaging .ImageAttributes

Lawmaking language: CSS ( css )

Then call one or more of the ImageAttributes class'due south methods:

  • SetWrapMode – Specifies the wrap way that is used to decide how to tile a texture across a shape. This attribute is used with textured brushes (a topic that isn't discussed in this tutorial).
  • SetGamma – This method sets the gamma value for the image'due south colors and accepts a Unmarried value, which is the gamma value to be applied. A gamma value of 1 doesn't affect the colors of the prototype. A smaller value darkens the colors, whereas a larger value makes the image colors brighter. Notice that the gamma correction isn't the same as manipulating the brightness of the colors. The gamma correction takes into consideration the entire range of values in the epitome; it doesn't apply equally to all the colors. In effect, information technology takes into consideration both the brightness and the contrast and corrects them in tandem with a fairly complicated algorithm. The syntax of the SetGamma method is equally follows:
                  Dim attr As New System.Drawing.Imaging.ImageAttributes            

The following statements render the image stored in the img Image object on the G Graphics object, and they gamma-right the image in the process by a factor of ane.25:

          

Dim attrs As New Arrangement .Drawing .Imaging .ImageAttributes() attrs .SetGamma(1.25) Dim dest As New Rectangle(0, 0, PictureBox1 .Width, PictureBox1 .Height) G .DrawImage(img, dest, 0, 0, img .Width, img .Pinnacle, _ GraphicsUnit .Pixel, attrs)

Lawmaking language: CSS ( css )

greeneliled1959.blogspot.com

Source: https://www.w3computing.com/vb2008/vb-drawing-methods-gdi/

0 Response to "Vb Draw Box Around Group of Tables"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel