Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Check if GDI+ bitmap has transparency

Check if GDI+ bitmap has transparency

Scheduled Pinned Locked Moved Graphics
graphicsquestionwinformshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TrekkieDK
    wrote on last edited by
    #1

    I just posted this in the VB group, but then realized that this is most likely the proper place to post my question. :) Here it goes: I have opened an image as a bitmap using GDI+. Before I save it I want to know if the image has transparency in it. That is; is there any pixels in the bitmap that has the transparent color? I have made a solution that looks like this:

    Dim objImage As Drawing.Bitmap = Drawing.Image.FromFile("png_with_transparent_bg.png")
    Dim booTransparentColorFound As Boolean = False
    If Bitmap.IsAlphaPixelFormat(objImage.PixelFormat) Then
    For y As Integer = 0 To objImage.Height - 1
    If booTransparentColorFound Then Exit For
    For x As Integer = 0 To objImage.Width - 1
    Dim objColor As Drawing.Color = objImage.GetPixel(x, y)
    If objColor.A = Color.Transparent.A And _
    objColor.R = Color.Transparent.R And _
    objColor.G = Color.Transparent.G And _
    objColor.B = Color.Transparent.B _
    Then
    booTransparentColorFound = True
    Exit For
    End If
    Next
    Next
    End If

    The problem is that if the image is very big, and there is no transparent pixel in it (or a transparent pixel is located in the lower right corner), the routine is very slow. Is there some quicker way to determine if there is transparency in the image? Thanks in advance. ...Allan, Denmark

    M L 2 Replies Last reply
    0
    • T TrekkieDK

      I just posted this in the VB group, but then realized that this is most likely the proper place to post my question. :) Here it goes: I have opened an image as a bitmap using GDI+. Before I save it I want to know if the image has transparency in it. That is; is there any pixels in the bitmap that has the transparent color? I have made a solution that looks like this:

      Dim objImage As Drawing.Bitmap = Drawing.Image.FromFile("png_with_transparent_bg.png")
      Dim booTransparentColorFound As Boolean = False
      If Bitmap.IsAlphaPixelFormat(objImage.PixelFormat) Then
      For y As Integer = 0 To objImage.Height - 1
      If booTransparentColorFound Then Exit For
      For x As Integer = 0 To objImage.Width - 1
      Dim objColor As Drawing.Color = objImage.GetPixel(x, y)
      If objColor.A = Color.Transparent.A And _
      objColor.R = Color.Transparent.R And _
      objColor.G = Color.Transparent.G And _
      objColor.B = Color.Transparent.B _
      Then
      booTransparentColorFound = True
      Exit For
      End If
      Next
      Next
      End If

      The problem is that if the image is very big, and there is no transparent pixel in it (or a transparent pixel is located in the lower right corner), the routine is very slow. Is there some quicker way to determine if there is transparency in the image? Thanks in advance. ...Allan, Denmark

      M Offline
      M Offline
      Matthew Butler 0
      wrote on last edited by
      #2

      There are two things you could do (first way will not speed up the processing but is just a method improvement): 1) Only need to check the alpha channel (assuming any transparent colour is counted)... If objColor.A != 255 Then ... (As the alpha channel IS the 'trasparency' aspect of the colour, you only need to check this). 2) Lock the Bitmap and loop through the raw data (this is REALLY fast)... loads of examples on web... from msdn[^] Also... if all you want to know is if the image could contain transparency... check the PixelFormat of the bitmap for transparency. Hope this helps.

      Matthew Butler

      1 Reply Last reply
      0
      • T TrekkieDK

        I just posted this in the VB group, but then realized that this is most likely the proper place to post my question. :) Here it goes: I have opened an image as a bitmap using GDI+. Before I save it I want to know if the image has transparency in it. That is; is there any pixels in the bitmap that has the transparent color? I have made a solution that looks like this:

        Dim objImage As Drawing.Bitmap = Drawing.Image.FromFile("png_with_transparent_bg.png")
        Dim booTransparentColorFound As Boolean = False
        If Bitmap.IsAlphaPixelFormat(objImage.PixelFormat) Then
        For y As Integer = 0 To objImage.Height - 1
        If booTransparentColorFound Then Exit For
        For x As Integer = 0 To objImage.Width - 1
        Dim objColor As Drawing.Color = objImage.GetPixel(x, y)
        If objColor.A = Color.Transparent.A And _
        objColor.R = Color.Transparent.R And _
        objColor.G = Color.Transparent.G And _
        objColor.B = Color.Transparent.B _
        Then
        booTransparentColorFound = True
        Exit For
        End If
        Next
        Next
        End If

        The problem is that if the image is very big, and there is no transparent pixel in it (or a transparent pixel is located in the lower right corner), the routine is very slow. Is there some quicker way to determine if there is transparency in the image? Thanks in advance. ...Allan, Denmark

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        You will have to use C# and go unsafe for better performance.

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups