Add Picture to Project and call it from code?

  • Thread starter Thread starter John Rugo
  • Start date Start date
J

John Rugo

Hi all,
Does anyone know how to add pictures to the VB.NET project and
programmatically call a desired picture and place it in a picture box
dynamically.

I could use an imagelist object but the scale is always different per
picture and the imagelist skews it.

I thought there was a way to add a picture, similar to adding
forms/module/classes/etc... to a project

Thanks,
John.
 
Goto Project -->Add Existing Item

Choose "Image Files" from the file type list

Choose your image and select "OK"

Then click on the item in the Solution Explorer and set the Build Action in
the Properties to be "Embedded Resource"

You can now access the image as a Stream using the following...

System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(na
me of file)
 
* "John Rugo said:
Does anyone know how to add pictures to the VB.NET project and
programmatically call a desired picture and place it in a picture box
dynamically.

I could use an imagelist object but the scale is always different per
picture and the imagelist skews it.

I thought there was a way to add a picture, similar to adding
forms/module/classes/etc... to a project

See:

<http://www.google.de/[email protected]>
 
Thank you; but how do I write 'access the image as a Stream using'?

Goto Project -->Add Existing Item

Choose "Image Files" from the file type list

Choose your image and select "OK"

Then click on the item in the Solution Explorer and set the Build Action in
the Properties to be "Embedded Resource"

You can now access the image as a Stream using the following...

System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(na
me of file)
 
Once you have your image added as a resource use something like the
following....

' Read the resource into a stream
Dim str As IO.Stream =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
"MyApp.MyPic.bmp")
' Read the data into a byte array
Dim buffer(str.Length) As Byte
str.Read(buffer, 0, str.Length)
' Convert the byte array to an image
Dim img As New ImageConverter()
Dim bmp As Image = img.ConvertFrom(buffer)
' Show the image in a picture box
PictureBox1.Image = bmp

I tried this just to make sure it works. Here are a couple of things to
note...

1. The call to GetManifestResourceStream requires the name of the app
prepended to the image name
2. This name also seems to be case dependent so MyApp.Find.bmp is not the
same as MyApp.FIND.bmp
3. If all else fails, use something like the following to see the names of
the embedded resources

Dim s() As String =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
Dim s2 as String
For Each s2 in s
Debug.WriteLine(s2)
Next
 
Back
Top