790 likes | 847 Views
Fun with GDI+. Introducing GDI+ in VB .NET. Rod Stephens RodStephens@vb-helper.com http://www.vb-helper.com http://www.vb-helper.com/fun_with_gdi.html. Roadmap. What is GDI+? Drawing Image Processing Summary. What is GDI+?. GDI = Graphic Device Interface GDI+ = GDI.NET.
E N D
Fun with GDI+ Introducing GDI+ in VB .NET Rod Stephens RodStephens@vb-helper.com http://www.vb-helper.com http://www.vb-helper.com/fun_with_gdi.html Denver Visual Studio Users Group
Roadmap • What is GDI+? • Drawing • Image Processing • Summary Denver Visual Studio Users Group
What is GDI+? • GDI = Graphic Device Interface • GDI+ = GDI.NET Denver Visual Studio Users Group
GDI+ Namespaces • System.Drawing • System.Drawing.Drawing2D • System.Drawing.Imaging • System.Drawing.Text • System.Drawing.Printing • System.Drawing.Design Denver Visual Studio Users Group
Classes Graphics Pen, Pens Brush, Brushes Image Bitmap Icon Metafile Font, FontFamily Region Structures Color Point, PointF Rectangle, RectangleF Size, SizeF System.Drawing Denver Visual Studio Users Group
Classes HatchBrush LinearGradientBrush Blend PathGradientBrush ColorBlend GraphicsPath Line cap classes Matrix Enums DashCap, DashStyle LineCap, LineJoin HatchStyle System.Drawing.Drawing2D Denver Visual Studio Users Group
System.Drawing.Imaging • ImageFormat bmp, emf, exif, gif, icon, jpeg, memory bitmap, png, tiff, wmf • ColorMap, ColorMatrix, ColorPalette • Metafile, MetafileHeader, MetaHeader • WmfPlaceableFileHeader Denver Visual Studio Users Group
System.Drawing.Text • FontCollection • InstalledFontCollection • PrivateFontCollection Denver Visual Studio Users Group
InstalledFontCollection ' Get the installed fonts collection. Dim installed_fonts As New InstalledFontCollection() ' Get the font familiies. Dim font_families() As FontFamily = installed_fonts.Families() ' Display the font families. Dim txt As String Dim i As Integer For i = 0 To font_families.Length - 1 txt &= font_families(i).Name & vbCrLf Next i TextBox1.Text = txt Example: ListFonts Denver Visual Studio Users Group
System.Drawing.Printing • PrintDocument • Property classes • Margins • PageSettings • PaperSize • PaperSource • PrinterResolution • PrinterSettings Denver Visual Studio Users Group
System.Drawing.Design • ToolboxItem • Property editors • BitmapEditor • FontEditor • ImageEditor Denver Visual Studio Users Group
Roadmap • What is GDI+? • Drawing • Image Processing • Summary Denver Visual Studio Users Group
Drawing • VB 6 and VB .NET approaches • Specifying colors • Getting Graphics Objects • Pens and Brushes Denver Visual Studio Users Group
Drawing in VB 6 • Set properties • Call a drawing method Denver Visual Studio Users Group
Ellipse in VB6 Private Sub cmdDrawEllipse_Click() Me.DrawWidth = 3 Me.FillColor = vbBlue Me.FillStyle = vbDownwardDiagonal Me.Circle (ScaleWidth / 2, ScaleHeight / 2), _ ScaleWidth * 0.45, vbRed, , , _ ScaleHeight / ScaleWidth End Sub Denver Visual Studio Users Group
Drawing in VB .NET • Create a drawing object (Graphics) • Create drawing property objects (Pen and Brush) • Call drawing object methods Denver Visual Studio Users Group
Ellipse in VB .NET Imports System.Drawing.Drawing2D ... Private Sub btnDrawEllipse_Click(...) ... Dim gr As Graphics = Me.CreateGraphics() Dim ellipse_brush As New HatchBrush( _ HatchStyle.BackwardDiagonal, _ Color.Blue, Me.BackColor) gr.FillEllipse(ellipse_brush, 0, 0, _ Me.ClientSize.Width, Me.ClientSize.Height) Dim ellipse_pen As New Pen(Color.Red, 5) gr.DrawEllipse(ellipse_pen, 0, 0, _ Me.ClientSize.Width, Me.ClientSize.Height) End Sub Denver Visual Studio Users Group
Specifying Colors • Named colors • Color.Red • By name • Color.FromName("DarkMagenta") • From ARGB values • Color.FromArgb(a, r, g, b) Denver Visual Studio Users Group
Getting Graphics Objects • Paint event handler • CreateGraphics Denver Visual Studio Users Group
Paint Event Handler ' Draw lines using the Graphics object ' provided by the event handler. Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint ' Create a Pen. Dim orange_pen As New Pen(Color.Orange, 3) ' Draw lines. e.Graphics.DrawLine(orange_pen, _ 0, 0, ClientSize.Width, ClientSize.Height) e.Graphics.DrawLine(orange_pen, _ 0, ClientSize.Height, ClientSize.Width, 0) e.Graphics.DrawRectangle(orange_pen, _ 1, 1, ClientSize.Width - 3, ClientSize.Height - 3) End Sub Example: Paint Denver Visual Studio Users Group
Paint Event Handler (2) Denver Visual Studio Users Group
CreateGraphics Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint ' Create a Pen. Dim orange_pen As New Pen(Color.Orange, 3) ' Create a Graphics object. Dim gr As Graphics = CreateGraphics() ' Draw lines. gr.DrawLine(orange_pen, _ 0, 0, ClientSize.Width, ClientSize.Height) gr.DrawLine(orange_pen, _ 0, ClientSize.Height, ClientSize.Width, 0) gr.DrawRectangle(orange_pen, _ 1, 1, ClientSize.Width - 3, ClientSize.Height - 3) End Sub Example: Paint2 Denver Visual Studio Users Group
CreateGraphics (2) ' To erase before drawing, add this. gr.FillRectangle( _ New SolidBrush(Me.BackColor), _ Me.DisplayRectangle) Denver Visual Studio Users Group
Graphics Methods • DrawArc • DrawBezier, DrawBeziers • DrawClosedCurve, DrawCurve • DrawEllipse • DrawIcon, DrawIconUnstretched, DrawImage, DrawImageUnscaled • DrawLine, DrawLines • DrawPath • DrawPie • DrawPolygon • DrawRectangle, DrawRectangles • DrawString Denver Visual Studio Users Group
Invalidate • A control’s Invalidate method sends a Paint event covering the entire control Denver Visual Studio Users Group
Pens and Brushes • Brushes • Stock Brushes • SolidBrush • HatchBrush • TextureBrush • LinearGradientBrush • PathGradientBrush • Pens • Stock Pens Example: StockPensAndBrushes Denver Visual Studio Users Group
Stock Pens and Brushes Dim rect As New Rectangle(10, 10, _ Me.ClientSize.Width - 20, _ Me.ClientSize.Height - 20) ' Fill a rectangle with a solid Aqua brush. e.Graphics.FillRectangle(Brushes.Aqua(), rect) ' Outline the rectangle with 1-pixel line. e.Graphics.DrawRectangle(Pens.Blue(), rect) Example: StockPensAndBrushes Denver Visual Studio Users Group
SolidBrush Dim rect As New Rectangle(10, 10, _ Me.ClientSize.Width - 20, _ Me.ClientSize.Height - 20) ' Fill a rectangle with a solid magenta brush. e.Graphics.FillRectangle( _ New SolidBrush(Color.Magenta), rect) ' Outline the rectangle with 10-pixel line. e.Graphics.DrawRectangle( _ New Pen(Color.DarkMagenta, 10), rect) Example: SolidBrush Denver Visual Studio Users Group
HatchBrush ' Fill a closed curve a hatched brush. e.Graphics.FillClosedCurve( _ New HatchBrush(HatchStyle.LargeConfetti, _ Color.Blue, Color.LightBlue), _ pts) ' Make a pen. Dim a_pen As New Pen(Color.DarkSlateBlue, 3) Dim dash_pattern As Single() = {10, 2, 5, 2} a_pen.DashPattern = dash_pattern a_pen.DashStyle = DashStyle.Custom ' Outline the closed curve. e.Graphics.DrawClosedCurve(a_pen, pts) Example: HatchBrush Denver Visual Studio Users Group
TextureBrush Dim pts() As Point = { _ New Point(20, 5), ... , New Point(5, 60) _ } ' Fill a polygon with a textured brush. e.Graphics.FillPolygon( _ New TextureBrush(picFace.Image), pts) ' Make a pen. Dim a_pen As New Pen(Color.Tomato, 3) a_pen.DashStyle = Drawing.Drawing2D.DashStyle.Dash ' Outline the polygon. e.Graphics.DrawPolygon(a_pen, pts) Example: TextureBrush Denver Visual Studio Users Group
LinearGradientBrush (1) Dim txt = " VB" & vbCrLf & ".NET" Dim font_size As Single = 64 Dim text_path As New GraphicsPath() Dim font_family As New FontFamily("Comic Sans MS") ' Add text to the path. text_path.AddString(txt, font_family, _ System.Drawing.FontStyle.Bold, _ font_size, New PointF(5, 5), _ System.Drawing.StringFormat.GenericDefault) ' Get the text's width and height. Dim txt_size As SizeF = _ e.Graphics.MeasureString(txt, _ New Font(font_family, font_size, _ FontStyle.Bold, GraphicsUnit.Pixel)) Example: LinearGradientBrush Denver Visual Studio Users Group
LinearGradientBrush (2) ' Fill the path with a gradient. e.Graphics.FillPath( _ New System.Drawing.Drawing2D.LinearGradientBrush( _ New Point(5, 5), _ New Point(5 + txt_size.Width, 5 + txt_size.Height), _ Color.Red, _ Color.Blue), _ text_path) ' Outline the path. e.Graphics.DrawPath(Pens.Black, text_path) Example: LinearGradientBrush Denver Visual Studio Users Group
PathGradientBrush (1) Dim pts() As Point = { ... } Dim curve_path As New GraphicsPath() curve_path.AddClosedCurve(pts, 0.5) ' Define the brush. Dim curve_brush As New _ System.Drawing.Drawing2D.PathGradientBrush(curve_path) Dim surround_colors() As Color = {Color.Blue} curve_brush.SurroundColors = surround_colors curve_brush.CenterColor = Color.Green ' Fill the shape. e.Graphics.FillPath(curve_brush, curve_path) ' Outline the shape. e.Graphics.DrawPath(Pens.Black, curve_path) Example: PathGradientBrush Denver Visual Studio Users Group
PathGradientBrush (2) curve_brush.CenterPoint = _ New PointF(wid * 0.4, hgt * 0.4) Example: PathGradientBrush2 Denver Visual Studio Users Group
Roadmap • What is GDI+? • Drawing • Image Processing • Summary Denver Visual Studio Users Group
VB .NETImage Processing Rocks! • Using memory bitmaps • Saving GIFs and JPEGs • Performing pixel-by-pixel operations • Rotating and transforming images • Alpha blending and transparency Denver Visual Studio Users Group
Loading Images ' Compose the picture's file name. Dim file_name As String = Application.ExecutablePath file_name = _ file_name.Substring(0, file_name.LastIndexOf("\bin")) & _ "\VBHelper2.jpg" ' Load the picture into a Bitmap. Dim bm As New Bitmap(file_name) ' Display the results. picImage.Image = bm picImage.SizeMode = PictureBoxSizeMode.AutoSize Example: LoadPicture Denver Visual Studio Users Group
Memory Bitmaps ' Make a Bitmap matching the PictureBox's size. Dim bm As New Bitmap(picImage.Width, picImage.Height) ' Make a Graphics object to draw on the Bitmap. Dim gr As Graphics = Graphics.FromImage(bm) ' Draw on the Graphics object. gr.FillRectangle(...) ' Display the result. picImage.Image = bm Denver Visual Studio Users Group
Saving Images ' Get a Bitmap. Dim bm As Bitmap = picImage.Image ' Save the picture as a bitmap, JPEG, and GIF. bm.Save(file_name & "bmp", _ System.Drawing.Imaging.ImageFormat.Bmp) bm.Save(file_name & "jpg", _ System.Drawing.Imaging.ImageFormat.Jpeg) bm.Save(file_name & "gif", _ System.Drawing.Imaging.ImageFormat.Gif) Example: SavePicture Denver Visual Studio Users Group
Save Parameters • Destination • String • Stream • System.Drawing.Imaging.ImageFormat • Bmp • Emf • Exif • Guid • Icon • Jpeg • MemoryBmp • Png • Tiff • Wmf Denver Visual Studio Users Group
Pixel-by-Pixel Operations ' Get the bitmap and its dimensions. Dim bm As Bitmap = picImage.Image xmax = bm.Width - 1 ymax = bm.Height - 1 ' Convert the pixels to grayscale. For y = 0 To ymax For x = 0 To xmax ' Convert this pixel. With bm.GetPixel(x, y) clr = 0.3 * .R + 0.5 * .G + 0.2 * .B End With bm.SetPixel(x, y, _ Color.FromArgb(255, clr, clr, clr)) Next x Next y ' Display the results. picImage.Image = bm Example: PixelByPixel Denver Visual Studio Users Group
Graphics.DrawImage • 30 overloaded versions • Copy an image onto another image • Copy part of an image • Copy part of an image to a particular location, possibly resizing it • Map an image’s corners Denver Visual Studio Users Group
Copy an image onto another image (1) ' Note that fr_bm and to_bm must be different. Dim fr_bm As New Bitmap(picImage.Image) Dim to_bm As New Bitmap(picImage.Image) ' Get a Graphics object for fr_bm. Dim gr As Graphics = Graphics.FromImage(to_bm) ' Copy the image and a box around it. Dim rect As New Rectangle( _ fr_bm.Width * 0.1, fr_bm.Height * 0.1, _ fr_bm.Width * 0.4, fr_bm.Height * 0.4) gr.DrawImage(fr_bm, rect) gr.DrawRectangle(Pens.Red, rect) ' Display the result. picImage.Image = to_bm Example: DrawImageCopy Denver Visual Studio Users Group
Copy an image onto another image (2) Example: DrawImageCopy Denver Visual Studio Users Group
Copy part of an image (1) ' Get the Bitmaps and a Graphics object ' for the destination Bitmap. Dim fr_bm As New Bitmap(picImage.Image) Dim to_bm As New Bitmap(picImage.Image) Dim gr As Graphics = Graphics.FromImage(to_bm) ' Get source and destination rectangles. Dim fr_rect As New Rectangle(240, 20, 130, 100) Dim to_rect As New Rectangle(10, 10, 130, 100) ' Draw from the source to the destination. gr.DrawImage(fr_bm, to_rect, fr_rect, GraphicsUnit.Pixel) gr.DrawRectangle(Pens.Red, to_rect) ' Display the results. picImage.Image = to_bm Example: DrawImageCopyPart Denver Visual Studio Users Group
Copy part of an image (2) Example: DrawImageCopyPart Denver Visual Studio Users Group
Map an image’s corners (1) ' Get the Bitmaps and Graphics object. Dim fr_bm As New Bitmap(picImage.Image) Dim to_bm As New Bitmap(fr_bm.Width, fr_bm.Height) Dim gr As Graphics = Graphics.FromImage(to_bm) ' Fill the destination Bitmap with a background. gr.FillRectangle(Brushes.PapayaWhip, _ to_bm.GetBounds(System.Drawing.GraphicsUnit.Pixel)) ' Define the destination paralelogram. Dim dest_pts() As Point = { _ New Point(150, 0), _ New Point(fr_bm.Width, 50), _ New Point(0, fr_bm.Height - 50) _ } ' Draw the image. gr.DrawImage(fr_bm, dest_pts) Example: DrawImageMapCorners Denver Visual Studio Users Group
Map an image’s corners (2) UL Example: DrawImageMapCorners UR LL Denver Visual Studio Users Group
Map an image’s corners (3) Dim dest_pts() As Point = { _ New Point(fr_bm.Width, 50), _ New Point(150, 0), _ New Point(fr_bm.Width - 150, fr_bm.Height) _ } Example: DrawImageMapCorners UR UL LL Denver Visual Studio Users Group
Image Rotation (1) 1 2 2 1 Example: Rotate 3 3 Denver Visual Studio Users Group