• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

Retaining original Bitmap/GIF size in an imagelist

I have loaded several graphics into an imagelist. The dimensions of each graphic varies.
I have set imglist.imagesize to 120,120, but some of the graphics are 80,80  or 160,80 etc.
I use the imglist to load graphics into pictureboxes that are created at runtime. I wish to set the size of the picturebox based on the 'original' sizing of the graphic.
The problem appears to be that the dimensions are always 120,120.
Even if the item has physical dimensions of 80, 80 and I set the picturebox size to this, I get a 120,120 picture box.
I even tried setting the imglist.imagesize during runtime based on the physical dimensions to no avail


picBox.Size = New Size(imlRoom.Images(4).PhysicalDimension.Height, imlRoom.Images(4).PhysicalDimension.Width)
picBox.Image = imlRoom.Images(4)

How do I dimension and populate the picture box to show the original sizing. If there is another was beside imglist I am happy to go down another track, but the images need to be stored in the program. Thanks for any input.
1 Solution
If you don't have too many images, you could add them as resources to the project.

Here is one example Google brought up:
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or if you don't have too many images and they are small, you can load them into invisible PictureBoxes.

Milkus1Author Commented:
I think I will go with Idle mind's idea, but the problem is that when i assign properties to my dynamically created controls I get problems
  picbox = pbItem1

the new control actually becomes the hidden picturebox. The hidden one is gone, therefore I can only use it once.
If I try

picbox.image = pbItem1.image
picbox.size = new size (pbitem1.height,pbItem1.width)

I get a blank picture box that is not scaled? Is there a way of cloning all properties of the 'hidden' picturebox to the new one, without losing it?

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Mike TomlinsonMiddle School Assistant TeacherCommented:
I didn't have any problems getting it to work:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim picbox As New PictureBox
        picbox.SizeMode = PictureBoxSizeMode.AutoSize
        picbox.Location = New Point(8, 40)
        picbox.Image = pbItem1.Image ' take image from hidden one
    End Sub

pbItem1 was my hidden PictureBox.

I agree. The best way to do this is with Resources. Here is a pre-built function to get a stroed image from the project:

    Public Function GetResImage(ByVal Name As String) As Bitmap

        Dim im As System.IO.Stream = GetResStream(Name)
        'Dim nb As Bitmap = New Bitmap(im)

        Return New Bitmap(im)

    End Function

    Public Function GetResStream(ByVal Name As String) As IO.Stream


        Return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(Application.ProductName & "." & Name)

    End Function

You would call this like:

Image1.Image = GetResImage("YourImageName.gif")

You will, however, have to set the Build Action on your embedded images to Embedded Resource.
Milkus1Author Commented:
I have tried IdleMind's suggestion, and I got it to work so I can add multiple controls which i can rotate. Thanks!
The issue is that the graphics seem very unstable, If I have a rotated picture (eg: 90' rotation) and then drag another picture box over it the picture reverts back to the initial view or the graphic may be half and half. If I minimize the screen all the graphics go back to the original setting.
Is the other way more stable.? Or does it come down to the rotation method -

Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi Milkus1,

I'm not having any problems with rotated graphics in a picturebox as you describe.

Can you gave us a little more detail into what you are doing or supply some code?

Milkus1Author Commented:
Thanks for the response IdleMind.
I have figured out partly what was occurring, setting pbx to the sender seemed to cause the 'static' picturebox to rotate (the one it copied the original graphic from).
If I had rotated the figure 90', then created a new picturebox of the same graphic the dimension were reversed
I therefore tried the following code, having an intermediate picturebox that the 'rotated' image was taken from. This seemed to help. I also changed from autostretch to autosize when a new picturebox was created.

What I am left with is the graphic being proportional when a new one is created, but the rotation will mirror that of the last one made of that chosen graphic.
eg: If I make a pbx ^, rotate it >, then the next one I make is >. I would like it ^ (hope it makes sense)

The last issue is the stability of the graphics in a new pbx, if I drag a pbx1 over pbx2, then some of the time the graphic in pbx2 becomes distorted,rotates back to the original state, or is half and half.

Code for rotating

    Private Sub picBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim pbx As PictureBox = CType(sender, PictureBox)

        If e.Button = MouseButtons.Left Then
            startPoint = pbx.PointToScreen(New Point(e.X, e.Y))
            picFlip.Image = pbx.Image

            If pbx.Width = pbx.Height Then
                pbx.Image = picFlip.Image
                picFlip.Height = pbx.Width
                picFlip.Width = pbx.Height
                pbx.Height = picFlip.Height
                pbx.Width = picFlip.Width
                pbx.Image = picFlip.Image
            End If
        End If

    End Sub

Code for new control..

    Private Sub AddNewFRNControl(ByVal psType As String, ByVal psTag As String)
        Dim picBox As New PictureBox

        picBox.SizeMode = PictureBoxSizeMode.AutoSize
        Select Case psType
            Case "FRN"
                Select Case miFurn
                    Case 1
                        picBox.Size = New Size(picFrnMPWIII.Height * miRatio \ 100, picFrnMPWIII.Width * miRatio \ 100)
                        picBox.Image = picFrnMPWIII.Image
                        picBox.AccessibleDescription = "MPWIII (1000 x 1000)"

                End Select
        End Select

        picBox.Location = New Point(picRoom.Width / 2 - 10, picRoom.Height / 2 - 10)
'tag for tooltip
        picBox.Tag = psTag
'add to container

        AddHandler picBox.MouseDown, AddressOf picBox_MouseDown
        AddHandler picBox.MouseUp, AddressOf picBox_MouseUp
        AddHandler picBox.MouseMove, AddressOf picBox_MouseMove
        AddHandler picBox.MouseHover, AddressOf picBox_MouseHover
   End Sub

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now