Results 1 to 5 of 5

Thread: Image vs Bitmap

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Image vs Bitmap

    Hello!

    I wanted to know what is best to use for a picturebox.image - a bitmap variable or an Image variable. I have embedded resources (.png, .ico, and .jpg). I access them through calling a function from a class.

    Code:
    Public Class Items
    
        '-----------------------------------------------------------------------------------------------------------------------------------------------------------
        'ITEM
        Public Function Get_Item(ByRef ItemName As String) as Bitmap 'or As Image
    
            Dim Item As Bitmap = New Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("I_Res." + LTrim$(ItemName) + ".ico"))' can change the Item type to Image
            Return Item
            Item.Dispose()
    
        End Function
        '-----------------------------------------------------------------------------------------------------------------------------------------------------------
    
    End Class

    So what is best to do?

    1) Declare an Image variable and get an image type from the resources (if it is "As Image")

    Code:
    Dim tmpImage As Image = (Items.Get_Item("Item1"))
    or

    2) Declare a Bitmap variable and get a bitmap from the resources? (if it is "As Bitmap")

    Code:
    Dim tmpImage As Bitmap=new bitmap(Items.Get_Item("Item1"))
    3)-Is it best to declare a variable first as above and then put it in picturebox (PictureBox1.Image=tmpImage)
    -Or I can just put it in the picturebox straight ahead? (PictureBox1.Image=new bitmap(Items.Get_Item("Item1"))

    4)Do I need to call Dispose on the variables in the Function?

    Thank you in advance!
    Last edited by dday9; Feb 29th, 2016 at 11:29 AM. Reason: Code Tags

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Image vs Bitmap

    The Bitmap class inherits the Image class so every Bitmap object IS an Image object. In fact, the Image class is abstract (declared MustInherit in VB) so every Image MUST be either a Bitmap or some other derived type. In most cases, any Image objects you work with will be Bitmaps.

    While it won't hurt, there's no point actually using the Bitmap type unless you need to use the members of that type. If all you intend to do is assign to the Image property of a PictureBox then an Image reference is fine, because that property is type Image. Generally speaking, you should use the most general type that you can, e.g. if you have a method that uses the Text property of some TextBoxes, you can use a Control reference instead of a TextBox reference because Text is a member of the Control class. That gives you the freedom to use other types derived from Control later without changing the code.

    With regards to your #3 question, I would suggest only using a variable if you would then use that variable multiple times or doing so makes the code significantly easier to read.

    With regards to your #4 question, don't think about this situation specifically because you then can't apply it later. Think about the general situation. Basically, the rule is that is you have an object with a Dispose method that you don't intend to use any further then you call that Dispose method. Is that the case for you here? Of course it's not. You expect the Image to be displayed in the PictureBox so you're still using it. Disposing it would mean that it couldn't be displayed. Would you dispose of a picture that you intended to display in your home? Of course not. You dispose of things when they're not going to be used any more. Programming is no different.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: Image vs Bitmap

    Thank You very much for clearing those things up for me.

    I'm a noob, well, not even a noob in programming, just doing it for hobby witch I very much enjoy, so I haven't studied it anywhere.

    So regarding Your answer for the 4th question my noobish line of thought was like this - I assign the Image to the variable Item in the function, the function returns it when I call it and stores the variables Item info in a picturebox (shows the picture). Later when I want to dispose the picture I don't say Item.Dispose I say PictureBox1.Image.Dispose... So I figured the Item wasn't needed anymore the moment it was returned from function and put in Picturebox. And funny enough it works, the picture displays. Of course I didn't notice any double up in memory usage when I didn't use the Dispose or any improvements there when I put that Dispose, but I can assure You that it works normally with that dispose there...

    Anyway, I'm going to remove it. I just hope that this doesn't mean that I have something seriously wrong with my app.

    What I didn't say is that Class Items is stored in a .dll resource file so I could avoid compiling all the resources in the .exe and make it one mega large file. Maybe that's why that dispose doesn't affect the picture display? Just my noobish thoughts =)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Image vs Bitmap

    It doesn't really matter where that Items class is located. A single object is still a single object, no matter where or how many times it's referenced.

    Think about this. My father, my mother's husband and my grandfather's son are all the same person, right? They are three different ways to refer to one person, not three separate people.

    That's how programming objects work. If you create an Image object in a DLL and then assign it to the Image property of a PictureBox in an EXE there is still just one Image object. Dispose it in one place and you dispose it in the other because it's the same object.

    This is from the documentation for the Image.Dispose method:
    Calling the Dispose method allows the resources used by this Image to be reallocated for other purposes.

    Call Dispose when you are finished using the Image. The Dispose method leaves the Image in an unusable state. After calling Dispose, you must release all references to the Image so the garbage collector can reclaim the memory that the Image was occupying.
    I wouldn't be surprised to learn that your Image would not be able to display correctly if the PictureBox was repainted after disposing. Regardless of what works and what doesn't, you're not supposed to dispose an object until you're finished with it so don't.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: Image vs Bitmap

    No arguing there.

    Did as You said.

    Thank You again for info and quick response!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width