none
Try...Catch of Graphics.FromImage() won't generate an exception and debugger breaks.

    Question

  • Hi.
    I'm currently using DrawImage methods in order to modify a picture that my application downloads from internet.
    As the picture isn't known in advance I sometimes have to deal with indexed images on which I can't draw.
    I've come up with a function that I can use to write it on a new bitmap. However the transparent part of the png's is lost so I try to avoid using it. 

    But then i'm given an error from visual basic if I try using a "Try...Catch" on the Graphics.FromImage() function.
    Isn't that supposed to simply generate an exception and use the "catch" part of my code? (screenshot below)

    Yannick

    Sunday, March 19, 2017 1:48 AM

Answers

  • Hi,

    do a check for an indexed pixelformat and decide by the result of this, if, or if not to create a graphics object:

    If (avatar..PixelFormat And Imaging.PixelFormat.Indexed) = Imaging.PixelFormat.Indexed Then
        'no graphics object
    Else
        'use a graphics object
    End If

    Regards,

      Thorsten



    Sunday, March 19, 2017 2:26 AM
  • You have enabled 'Break when this exception type is thrown' so the debugger will break and the Try/Catch will not be processed.

    Sunday, March 19, 2017 2:30 AM

All replies

  • Hi,

    do a check for an indexed pixelformat and decide by the result of this, if, or if not to create a graphics object:

    If (avatar..PixelFormat And Imaging.PixelFormat.Indexed) = Imaging.PixelFormat.Indexed Then
        'no graphics object
    Else
        'use a graphics object
    End If

    Regards,

      Thorsten



    Sunday, March 19, 2017 2:26 AM
  • You have enabled 'Break when this exception type is thrown' so the debugger will break and the Try/Catch will not be processed.

    Sunday, March 19, 2017 2:30 AM
  •  Instead of using a Try/Catch,  you could check the PixelFormat of the "avatar" Bitmap.  If it is an 8bppIndexed,  4bppIndexed, or 1BppIndexed,  then create a new 32bppArgb Bitmap image,  create the graphics object for the new Bitmap,  and then use the Graphics.DrawImage method to draw the Indexed image to the new Bitmap.

    For example....

            If (avatar.PixelFormat And Imaging.PixelFormat.Format1bppIndexed) = Imaging.PixelFormat.Indexed Then
                Dim bm As New Bitmap(avatar.Width, avatar.Height, Imaging.PixelFormat.Format32bppArgb)
                Using g As Graphics = Graphics.FromImage(bm)
                    g.DrawImage(avatar, 0, 0, avatar.Width, avatar.Height)
                End Using

                'do whatever with the new bitmap....

            End If

    There are a few ways to find the transparent pixels of an Indexed type image and then set the alpha channel of the pixels in the 32BppArgb image to 0 so that the Transparency it kept but,  that is another topic.  Also,  an indexed image is usually a Gif or Tiff image,  but not a Png image.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, March 19, 2017 9:19 AM
    Sunday, March 19, 2017 3:01 AM
  • Hi,

    do a check for an indexed pixelformat and decide by the result of this, if, or if not to create a graphics object:

    If (avatar..PixelFormat And Imaging.PixelFormat.Indexed) = Imaging.PixelFormat.Indexed Then
        'no graphics object
    Else
        'use a graphics object
    End If

    Regards,

      Thorsten



    Hi,

    just an addition, I forgot the parentheses. (They are needed to report both cases)

    Regards,

      Thorsten

    Sunday, March 19, 2017 3:44 AM
  • I was going to say that I tried with the box unchecked (and it still broke at this line) but after rebuilding the solution it doesn't do it anymore. Thanks ;)

    Thorsten Gudera : Your solution works perfectly, thanks too!

    IronRazerz : your condition doesn't seem to work but that's no problem as I've got one working.
    However I'd like to know more about how I can copy transparent pixels on my new bitmap. Do you have any documentation about it? I can't find anything...

    //Nevermind I figured a way to do it:

    Function convert(ByVal fsSource As IO.FileStream) As Bitmap
            Dim sourcepic= Image.FromStream(fsSource)
            Dim emptyBitmap As New Bitmap(sourcepic.Width, sourcepic.Height, Imaging.PixelFormat.Format32bppPArgb)
            emptyBitmap.MakeTransparent(Color.Black)
            Dim g As Graphics = Graphics.FromImage(emptyBitmap)
            g.DrawImage(sourcepic, 0, 0, sourcepic.Width, sourcepic.Height)
            Return emptyBitmap
        End Function



    • Edited by Yannick.D Sunday, March 19, 2017 12:48 PM
    Sunday, March 19, 2017 11:37 AM