MakeTransparent not seeming to do anything

Hi All,
I’m struggling getting a transparent image. PNG support, to my knowledge, is not there yet, so my only option is the Image.MakeTransparent() method. There are only a handful of posts about it, so I tried to follow those as closely as I could, but it doesn’t seem to be doing anything.

I rigged up this (very inefficient) test:

public static Image TestImage = Properties.Resources.GetBitmap(Properties.Resources.BitmapResources.Backbutton);

//this portion is part of a larger draw loop that calls every 100 ms or so
if(DrawBackT)
            {
                TestImage.MakeTransparent(Color.White); //test if i need to do this every draw call
                G.GraphicsScreen.DrawImage(TestImage, 14, 434);
                DrawBackT = false; 
            }
            else
            {
                G.GraphicsScreen.DrawImage(TestImage, 14, 434);
                DrawBackT = true; 
            }

my thought process is if the background is flicking on and off between white and transparent, that would tell me I need to run it before every draw call, if it stayed transparent, I would only need to run it once. Except, the white background stays put. I have verified that the pixels in the bitmap match a 255,255,255,255 ARGB color code, which matches the Color.White struct. I’ve tried 5 different bitmap encodings - instead, it just will sit there and mock me.

image

I tried the bucket trick in microsoft paint to make sure that all the pixels are actually all the same color and it worked fine.

Any tips? I’m sure that it’s something stupid I’m missing,

Hi,

We are sure it works because our “Official Demos” projects are using it. All of icons on screen are set to transparent. You can check our project:

https://github.com/ghi-electronics/TinyCLR-Samples/blob/master/Official%20Demos/SCM20260D%20Dev/Utils/Icon.cs

These project are using UI. If you don’t want UI, try this:

var graphics_trans = Graphics.FromImage(original_bitmap);

graphics_trans.MakeTransparent(Color.White);

var new_image_trans = new Bitmap(graphics_trans.GetBitmap(0, 0, graphics_trans.Width, graphics_trans.Height), graphics_trans.Width, graphics_trans.Height);

let us know your result.
Thanks.

Hi Dat,
Looks like it doesn’t seem to work, i just get a few black spots on the image.

Does the .bmp file have to be encoded a certain way?

Here is the image file I’m using.

I just wrote full code to test.

But, please verify network directly work then I will show you my code :))

Ah, holding me hostage.

I will get you something later today - it’s a bit of a chore updating all the libraries :smiley:

1 Like

:)).

Anyway, your original code is correct, just need to change

Color.White

to

System.Drawing.Color.FromArgb(0x00FFFFFF)

Below is full

var button = Resources.GetBitmap(Resources.BitmapResources.Backbutton);

            var gfxTrans = Graphics.FromImage(button);

            gfxTrans.MakeTransparent(System.Drawing.Color.FromArgb(0x00FFFFFF));
          
            screen.DrawImage(button, 50, 50);

            screen.Flush();
2 Likes

Edit: I’m super good at reading code - disregard this post.

P2P connections work, but alas, this code still does not.
image

this is the initialization code:

public static void Initialize()
        {
            Graphics temp;
            temp = Graphics.FromImage(DataDefs.backbutton_temp);
            temp.MakeTransparent(Color.FromArgb(0x00FFFFFF)); 
            DataDefs.backbutton = new Bitmap(temp.GetBitmap(0, 0, temp.Width, temp.Height), temp.Width, temp.Height);
}

I know it’s initializing because when I draw it later it works, and the backbutton object is null on startup.

Have you confirmed this works with the image I provided?

  1. Confirm your image work perfectly.
  2. You need to run my code first.

DataDefs.backbutton won’t be OK

new Bitmap(temp.GetBitmap(0, 0, temp.Width, temp.Height), temp.Width, temp.Height);

that line cause lost transparent again!
DataDefs.backbutton_temp will be OK when you draw it.

Oh crap, you’re right. I completely misread that, sorry. I blame infant-derived sleep deprivation.

1 Like