By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

Convert String to System.Drawing.Image

P: 1
VVVVVVVVVVVVVVVVVVVVVVVV Code

Expand|Select|Wrap|Line Numbers
  1. Private Sub impimg_Click(sender As Object, e As EventArgs) Handles impimg.Click
  2.         Dim fileimg As New OpenFileDialog
  3.         fileimg.Filter = "Bitmap Images (*.bmp)|*.bmp"
  4.         fileimg.Title = "Open Icon File"
  5.         fileimg.InitialDirectory = "C:\"
  6.         DialogResult = fileimg.ShowDialog
  7.         If DialogResult = Windows.Forms.DialogResult.OK Then
  8.             imglabel.Text = fileimg.FileName
  9.             finalinp.imgpic.Image = fileimg.FileName
  10.         End If
  11.     End Sub
  12.  
  13. Here
  14.  
  15.             finalinp.imgpic.Image = fileimg.FileName
is where I get the trouble I tried
Expand|Select|Wrap|Line Numbers
  1. Dim imgz As System.Drawing.Image = fileimg.FileName
but it didn't work.
Jun 8 '15 #1
Share this Question
Share on Google+
1 Reply


!NoItAll
100+
P: 296
Fileimg.filename returns a string (which you appear to know), but that string is not the image, rather it is a string such as "myicon.ico" and cannot be converted to the image you want.
Instead try the fromfile method of the system.drawing.image class.
Expand|Select|Wrap|Line Numbers
  1. Private Sub impimg_Click(sender As Object, e As EventArgs) Handles impimg.Click
  2.  
  3.         Dim myImage as System.drawing.image
  4.  
  5.         Dim fileimg As New OpenFileDialog
  6.         fileimg.Filter = "Bitmap Images (*.bmp)|*.bmp"
  7.         fileimg.Title = "Open Icon File"
  8.         fileimg.InitialDirectory = "C:\"
  9.         DialogResult = fileimg.ShowDialog
  10.  
  11.         If DialogResult = Windows.Forms.DialogResult.OK Then
  12.             imglabel.Text = fileimg.FileName
  13.             MyImage = Image.fromfile(fileimg.FileName)
  14.         End If
  15.  
  16.  
  17. End Sub
  18.  
Now that you have an image (MyImage) you can do something useful with it such as

Expand|Select|Wrap|Line Numbers
  1. Picturebox1.Image = MyImage
...and see the image. You can also place the imgLabel below the image so you can see the filename.
Jun 15 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.