Retaining original Bitmap/GIF size in an imagelist

Posted on 2004-11-23
Last Modified: 2012-05-05
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.
Question by:Milkus1
    LVL 6

    Expert Comment

    If you don't have too many images, you could add them as resources to the project.

    Here is one example Google brought up:
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Or if you don't have too many images and they are small, you can load them into invisible PictureBoxes.


    Author Comment

    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?
    LVL 85

    Accepted Solution

    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.

    LVL 1

    Expert Comment

    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.

    Author Comment

    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 -

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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?


    Author Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now