Results 1 to 8 of 8

Thread: Picturebox to bitmap? errors?!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Picturebox to bitmap? errors?!

    I am using this code:

    Dim img as bitmap = DirectCast(Picturebox1.Image, bitmap)

    --

    I keep getting this stupid error after i debug!! it's driving me crazy.

    how can i fix this?

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

    Re: Picturebox to bitmap? errors?!

    Um, what stupid error? It would be handy to know what it is in order to suggest how to fix it. The code you've posted will work if the Image of the PictureBox refers to a Bitmap object and not otherwise. Any errors due to that specifically would happen at that point though, while you seem to be implying that your error happens later. Please provide ALL the relevant information and error messages are always relevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Picturebox to bitmap? errors?!

    Quote Originally Posted by jmcilhinney View Post
    Um, what stupid error? It would be handy to know what it is in order to suggest how to fix it. The code you've posted will work if the Image of the PictureBox refers to a Bitmap object and not otherwise. Any errors due to that specifically would happen at that point though, while you seem to be implying that your error happens later. Please provide ALL the relevant information and error messages are always relevant.
    The image i am using is not a bitmap. I want to turn that image into a bitmap so that I can find the color of certain pixels at some coordinate.

    If there is a way to do the above without using bitmaps, I will do that instead.

    Here is a screenshot of the error:

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

    Re: Picturebox to bitmap? errors?!

    Casting is not a way to convert an object from one type to another. If the object is not a Bitmap then you can't cast it as a Bitmap. If you want to create a Bitmap then you will have to actually create a Bitmap object of the appropriate dimensions and then use GDI+ to draw the original Image onto the Bitmap.

    That said, I'm not sure that that is the immediate issue based on that error message. Have you actually read that Exception Assistant window and done what it says? There's a link on that that says "View Detail". Have you clicked it to view the details? It tells you to see the InnerException for details. Have you done that?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Picturebox to bitmap? errors?!

    Quote Originally Posted by jmcilhinney View Post
    Casting is not a way to convert an object from one type to another. If the object is not a Bitmap then you can't cast it as a Bitmap. If you want to create a Bitmap then you will have to actually create a Bitmap object of the appropriate dimensions and then use GDI+ to draw the original Image onto the Bitmap.

    That said, I'm not sure that that is the immediate issue based on that error message. Have you actually read that Exception Assistant window and done what it says? There's a link on that that says "View Detail". Have you clicked it to view the details? It tells you to see the InnerException for details. Have you done that?
    And if I want to use a png as my image?

    EDIT: even if i use a bitmap image i still get the error!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Picturebox to bitmap? errors?!

    Quote Originally Posted by yoni201 View Post
    And if I want to use a png as my image?
    Read the first paragraph of my previous post.
    Quote Originally Posted by yoni201 View Post
    even if i use a bitmap image i still get the error!
    Read the second paragraph of my previous post.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Picturebox to bitmap? errors?!

    Quote Originally Posted by jmcilhinney View Post
    Read the first paragraph of my previous post.Read the second paragraph of my previous post.
    Yes, I read it. it's hard to tell what the problem is.

    Could you send me a complete source of the working project?

    thanks.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Picturebox to bitmap? errors?!

    Quote Originally Posted by yoni201 View Post
    Yes, I read it. it's hard to tell what the problem is.
    If you want us to help you fix the problem then does it not make sense to provide us with all the relevant information that the IDE gives you to help you fix the problem? Do you expect a diagnosis from a doctor without explaining the symptoms? I wouldn't like to be your mechanic either.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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