Results 1 to 14 of 14

Thread: Getting a black (transparent) image instead of white

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Getting a black (transparent) image instead of white

    Hi (will post the image)
    I have a white png transpaent Image that I try to resize and make transparent.
    the problem is that I get returned with a black non transparent Image.
    Any ideas?
    Haven't tried without the encoder yet.
    It may make a difference
    Code:
     Using dbmp = New Bitmap(dWidth, dHeight)
                        Using sbmp As New Bitmap(strImageSource)
    
    
                                dbmp.MakeTransparent(System.Drawing.Color.White)
                                sbmp.MakeTransparent(System.Drawing.Color.White)
    
                       
    
                            sbmp.SetResolution(DPI, DPI)               
                            gr = Graphics.FromImage(dbmp)                    
                            gr.DrawImage(sbmp, 0, 0, dWidth, dHeight) 
    
                          
    
                      
                            Dim jpgEncoder As ImageCodecInfo
                         
                                jpgEncoder = clsgen.GetEncoder(ImageFormat.Png)
                         
    
    
                            ' Create an Encoder object based on the GUID
                            ' for the Quality parameter category.
                            ''''This may or may not make a difference on the black transparent image, M trying as I post it....''''
    
                            Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
                          
                            Dim myEncoderParameters As New EncoderParameters(1)
    
                            Dim myEncoderParameter As New EncoderParameter(myEncoder, 100L)  
                            myEncoderParameters.Param(0) = myEncoderParameter
    
                            Try
                                dbmp.Save(strImageDestination, jpgEncoder, myEncoderParameters)
    
    
    ''''''encoder function
     Protected Friend Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
    
            Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
            Dim codec As ImageCodecInfo
            For Each codec In codecs
                If codec.FormatID = format.Guid Then
                    Return codec
                End If
            Next codec
            Return Nothing
    
        End Function
    Attached Images Attached Images  
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Getting a black (transparent) image instead of white

    When you declare a New Bitmap (by size etc.) its pixels are all set to ARGB(0,0,0,0). That's transparent black, also known as Color.Empty. If you paint the bitmap concerned in a way that disallows transparency, transparent pixels will appear black.

    The Color.Transparent structure, on the other hand, is ARGB(0,255, 255, 255) or transparent white. You could convert the background to transparent white with Graphics.Clear(Color.Transparent).

    BB

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    Hi.
    So I only used the Graphics.Clear(Color.Transparent) that you suggested and it works. However I must remove the bitmap.MakeTransparent option as it will still color it black.

    Will try to add some text now from an image and see if I can get A transparent image with text.Will post if I get into issue(today if I can make it or Monday).

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    Hello again.
    I'm creating the image but I am not sure how I can test it as I have a B/W laser printer here.
    The problem is that whatever image I have (jpg,png...) if it is white it will always print the black letters (that I put somewhere in the white image) and everything else will be left non painted.
    I was expecting, at least on a jpg non transparent image, if I print it on a B/W with a "colored" paper that it will print all the white Image above the color paper. Instead I just get the black letters only.
    That is fine but I'm not sure of the result I would get on a color printer?Meaning, will it print all the white background in the paper also or ignore it and just print the blacks?
    Any experience on that?
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Getting a black (transparent) image instead of white

    no usual printer can print white color.

    does that answer your question?

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Getting a black (transparent) image instead of white

    Computer printers aren't much use for checking transparency. You are right, they do not print white.

    A simple way to check the transparent parts of an image is to use a checkerboard pattern as a background image. You can generate the checkerboard yourself at runtime, for example (using PictureBox1) like this.
    Code:
          Dim checkerSize As Integer = 6
          Dim bmp As New Bitmap(2 * checkerSize, 2 * checkerSize)
          Using g As Graphics = Graphics.FromImage(bmp)
             g.Clear(Color.FromArgb(255, 200, 200, 200))
             Using dark As New SolidBrush(Color.FromArgb(255, 160, 160, 160))
                g.FillRectangle(dark, checkerSize, 0, checkerSize, checkerSize)
                g.FillRectangle(dark, 0, checkerSize, checkerSize, checkerSize)
             End Using
          End Using
          PictureBox1.BackgroundImageLayout = ImageLayout.Tile
          PictureBox1.BackgroundImage = bmp
          
    'paint the image to be tested:
         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
         PictureBox1.Image = Image.FromFile("D:\pictures\ghost.png")
    example output:
    Name:  checkerGhost.jpg
Views: 1828
Size:  28.5 KB

  7. #7

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    thanks both.
    boops I'll give it a go on Monday. I believe I have somewhere in my code stashed a function for painting an image above the other so I would put them on the checkerboard and see what I will find.
    thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    So After a couple of month I have implemented transparency.
    What I come back with is to say that you may run into some issues if you try to give Smoothing and TextRendering mode the highest quality. It will make the image appear a little "fluffy".
    I was using this on an image that is created with characters, so the image is a "text image" and I was getting a blur effect on the text.
    So I was forced to downgrade the quality to .Default . Probably this not applies to an image that has pictures and not characters simulation. I only use transparent character-images above a standard Image that I png it. The png images can accept image high quality settings without an issues but the characters on the glued images appear blurred with those same settings.

    So no question just a hint.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Getting a black (transparent) image instead of white

    Graphics.SmoothingMode only applies to lines (Graphics.DrawLine, DrawEllipse etc.) and to the edges of shapes (Graphics.FillRectangle etc.). So probably it isn't relevant to your "fluffiness" problem.

    Graphics.TextRenderingHint applies only to font rendering, of course. The various graphics quality modes are all independent and don't interact.

    TTF fonts are stored as vectors, so in theory they can be rendered with optimal quality at any size, for example when you use Graphics.DrawString, or Graphics.DrawPath with a GraphicsPath that includes a String. The Default TextRenderingHint is good enough for many purposes, but ofther TextRenderingHints provide some options for fine-tuning on specific hardware.

    One thing that could cause blurring is if you render text to a bitmap, then scale up the bitmap. That way the font will be rendered as a raster instead of as a vector image. It shouldn't be necessary to do that, but if you do you should at least choose one of the high quality interpolation modes for scaling. HighQualityBicubic is usually best.

    BB

  10. #10

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    Hi.
    I use HighQualityBicubic and ClearTypeGridFit for text rendering. That will fluff the characters.
    I render text to a bitmap, true, and i do scale up the bitmap. I do that because I need a "white" sized background on non transparent images.High quality interpolation on transparent will cause the blur effect.High quality on non transparent (I guess,as you , say, as I can't test right now on transparent(have to change a couple of thou lines of code again),TextRenderingHint) will not fluff the image.

    Edit I use Bitmap.DrawString
    Last edited by sapator; Jan 11th, 2018 at 10:39 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Getting a black (transparent) image instead of white

    Here's the problem.

    The "fluff" around the text is made via a process called "alpha blending". The computer has to make the text fill "some" of the pixel, but we know you can't fill just "some" of the pixel. So it fudges. What happens is different depending on if the image has an alpha channel. Either way, the computer thinks about "how much" of the pixel will have the text color. Let's call that percentage "the ratio".

    If it has no alpha channel, it has to blend the text color with the color beneath it. You get a pixel that is "the ratio"% the text color and 1-"the ratio"% the background color. If the background color is transparent... well, you can't blend with transparent. The API probably picks white or black by default. That makes a picture that looks good against EITHER a light or dark background, but not both.

    If it has an alpha channel, that problem's solved. The text color is used but the alpha is set to "the ratio". So a pixel that's got "just a little" text on it has a very low opacity, and a pixel that has "a lot" of text color has high opacity, and blending as described above happens when the image is rendered so some sense of "the background" can be sorted out.

    So make sure your image has a pixel format that has an alpha channel like PixelFormat.Format32bppArgb. And don't bother testing the picture in a PictureBox or some other WinForms control. Transparency in Windows Forms is a nightmare. Test your image in a program like Paint.NET, where transparent backgrounds are displayed with a checkerboard and you can paste your image over a different one to see how the transparency is working.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  12. #12

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    Hi.
    Nice explanation.
    I always use 32bpp
    Code:
      Using Bmp As New Bitmap(parametersV.DWidth, parametersV.DHeight, Imaging.PixelFormat.Format32bppPArgb)
    and an 100% Quality JPG encoder and 96,96 DPI (if that makes any difference) . It may or may not be an issue that I present this on the Picturebox but i need to show to the user a screenshot before saving the image.
    Anyhow the default settings seems to work and I don't really need a super duper quality on the text part. In the end I paste every image part on a main Image that has top quality.
    Last edited by sapator; Jan 12th, 2018 at 07:19 AM. Reason: writing unthinkable stuff!
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  13. #13
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Getting a black (transparent) image instead of white

    The "JPG encoder" part bugs me too, but could just be inexperience with tweaking the aspects of how GDI saves an image.

    JPG doesn't support transparency at all, even if you say you want an ARGB palette. Maybe that's stripping your alpha channel before it goes to save.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  14. #14

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Getting a black (transparent) image instead of white

    No, sorry.

    I meant PNG ENCODER

    I switch that between transparent and non transparent.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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