Results 1 to 6 of 6

Thread: [RESOLVED] vb.net drawing question

  1. #1

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Resolved [RESOLVED] vb.net drawing question

    This is my attempt, but I can tell what I did was not good since this is my first time on vb.net

    Thanks
    Last edited by Toph; Feb 14th, 2015 at 05:16 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: vb.net drawing question

    Try this:
    Code:
    Dim bmp As Bitmap = New Bitmap(300, 300)
    Using g As Graphics = Graphics.FromImage(bmp)
        g.Clear(Color.White)
    
        Using rPen As Pen = New Pen(Color.Red)
            g.DrawRectangle(rPen, 10, 50, 10, 100)
        End Using
    
        Using fonty As New Font("Comic Sans MS", 10)
            Using rBrush As Brush = New SolidBrush(Color.Red)
                g.DrawString("Hello World", fonty, rBrush, New Point(120, 300))
            End Using
        End Using
    
        g.Save()
    End Using
    
    
    PictureBox1.Image = bmp
    Also, you may not want to set the Y position of the DrawString to 300. Instead you'll want to set the Y position to 300 minus the height of the string. You can do that by using Graphics.MeasureString:
    Code:
    Dim bmp As Bitmap = New Bitmap(300, 300)
    Using g As Graphics = Graphics.FromImage(bmp)
        g.Clear(Color.White)
    
        Using rPen As Pen = New Pen(Color.Red)
            g.DrawRectangle(rPen, 10, 50, 10, 100)
        End Using
    
        Using fonty As New Font("Comic Sans MS", 10)
            Using rBrush As Brush = New SolidBrush(Color.Red)
                g.DrawString("Hello World", fonty, rBrush, New Point(120, CInt(300 - g.MeasureString("Hello World", fonty).Height)))
            End Using
        End Using
    
        g.Save()
    End Using
    
    
    PictureBox1.Image = bmp
    But the main difference between mine and yours is that I:
    1. Use the Using statement on objects that implement IDisposable
    2. Create a bitmap by it's width and height rather than by a URL
    3. Save the graphics rather than the bitmap
    Last edited by dday9; Nov 28th, 2014 at 05:55 PM. Reason: Replaced Brushes.Red with rBrush.

  3. #3

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: vb.net drawing question

    Thanks Dday. Once again you have been of great help .

    Quick question. What is the point of declaring rBrush? it doesn't seem to do doing anything or am I wrong. You declared it in a using statement but never used it?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: vb.net drawing question

    I forgot to replace Brushes.Red with rBrush, which it should be. I've edited the code.

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,128

    Re: vb.net drawing question

    I don't see any reason why you shouldn't use Brushes.Red and be done with it. Predefined brushes in all the Web colors are part of System.Drawing so you won't save anything by trying to dispose of them (as in Using ... End Using).

    DDay's code is a reminder that it's considered a good design to dispose of Graphics objects, Pens, Brushes etc. after use This is because in theory they could hamper the work of the Garbage Collector (although I think that must only be a potential problem when the Brush or whatever is linked to an Image such as in a TextureBrush). End Using automatically disposes of the object concerned. But there is no point in doing that when a predefined brush is available. Apart from that, I like using Using blocks anyway because can make the code easier to read.

    A good reason for defining (and disposing of) your own solid color brush is when the color can change at run time. It is also useful for a non-web color:
    Code:
    Using sbr As New SolidBrush(Color.FromARGB(127, Color.Red))
    '...
    End Using
    which would give you a semi-transparent red brush.

    BB

  6. #6

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: vb.net drawing question

    Thanks for the help guys. Much appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width