XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

MarApril 2007May
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

Saturday, April 07, 2007 #

    I set myself the task of deleting a lot of old projects and solutions from my hard drive this holiday weekend to make room for all the new stuff that's coming down the pipeline.     Just as I was about to hit the OK button to confirm the Delete All, I remembered that in the past fortnight I've had thank-you emails from several readers who had found some of my early answers useful.   Those items had been written two and three years ago, so it occurred to me that, even though the stuff I was about to delete was far from new, maybe some of it would still be useful for people.

    So, I decided that I'll put some of the possibly interesting ones in the blog and if they come in handy for someone, that'll be a bonus

   This one came up a while back.   Someone wanted to display an image in a PictureBox, superimpose the image with a date, and save the changed image so that it could be reused later elsewhere.

   With GDI+ this is quite easy to do.   I'll include a link to a downloadable version of the project which uses VB.NET 2003 (but will convert to VB 2005 without problem).   The demo has one PictureBox and 5 Buttons.  

   I used a couple of variables to keep the hard-coded paths to a minimum.  (Would probably be better to store these as Resources in an application that's to be distributed).   These are:

Dim OriginalPic As Image = Image.FromFile("C:\Sunset.jpg")

Dim DatedPic As Image

  The first button simply loads the original image into the PictureBox:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PictureBox1.Image = OriginalPic

End Sub

  The second button adds the text to the image.   In this example it takes today's date and time, but of course any string can be used in this same way. 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim bmap As Bitmap  = New Bitmap(PictureBox1.Image)

Dim g As Graphics = Graphics.FromImage(bmap)

g.DrawString(Now.ToString, New Font("Arial", 9, FontStyle.Regular), Brushes.Black, 10, 33)

PictureBox1.Image = bmap

g.Dispose()

End Sub

   All that happens above is that an empty Bitmap is created and the current contents of the the PictureBox are assigned to that Bitmap.  Then a Graphics Object is created in order that we can use the GDI+ methods on this Bitmap.

   This Graphics Object is used to draw a string on top of the Bitmap (in this case at location 10, 33), using the font and font color of choice.   Once this change has been made we flip-flop the edited image back to the PictureBox by assigning bmap (now edited) as the PictureBox's Image.

   Finally, good housekeeping requires that we dispose of the Graphics Object once we have finished with it.

  The third button saves the edited image to the hard drive.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

PictureBox1.Image.Save("C:\Dated Pic.jpg")

DatedPic = Image.FromFile("C:\Dated Pic.jpg")

End Sub

   It really isn't necessary to include both these steps.  I just decided to do it that way so that I can repopulate the PictureBox later using the variable "DatedPic" instead of a hard-coded path.

  The fourth button is there to remove the image from the PictureBox so that we can prove to ourselves that the saved image is really available and has been permanently edited.

  Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

PictureBox1.Image = Nothing

End Sub

  And finally, that proof-of-the-pudding moment when we get the edited image from the hard drive and assign it as the PictureBox's image is handled by Button # 5:-

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

PictureBox1.Image = DatedPic

End Sub

   It's a fairly trivial task, as long as you grok the action that takes place in Button2 - the use of the Graphics object to grab the image, do something to it and then assign the tweaked image back to the PictureBox in its new clothes.

   The link to the demo solution is here.

posted @ 12:00 PM | Feedback (1)