.NET CF 2.0: Draw PNG on top of an image with background transparency

I want to draw an image over another without drawing its backgroud. The image I want to draw is a star. I want to put some stars above the map image.

The problem is that the star image has a white backgroud, and when I draw over the map, a white background appears.

My star drawing method is as follows:

Graphics graphics = Graphics.FromImage(map); 
Image customIcon = Image.FromFile("../../star.png");
graphics.DrawImage(customIcon, x, y);

I tried with transparent backgroud images (PNG and GIF formats) and it always draws something around the star. How to draw a star without a background?

The program is designed for Windows Mobile 5.0 and later with Compact Framework 2.0 SP2 and C #.

I tried with this code:

Graphics g = Graphics.FromImage(mapa);
Image iconoPOI = (System.Drawing.Image)Recursos.imagenPOI;
Point iconoOffset = new Point(iconoPOI.Width, iconoPOI.Height);

System.Drawing.Rectangle rectangulo;
ImageAttributes transparencia = new ImageAttributes();
transparencia.SetColorKey(Color.White, Color.White); 

rectangulo = new System.Drawing.Rectangle(x, y, iconoPOI.Width, iconoPOI.Height);
g.DrawImage(iconoPOI, rectangulo, x, y, iconoPOI.Width, iconoPOI.Height, GraphicsUnit.Pixel, transparencia);

But I don’t see anything on the map.

X Y - , iconoPOI, PNG .

!

+3
2

:

!

+1

( Windows BitBlt - ), .

, (bmpMap) (bmpStar), (xoffset, yoffset), , :

for (int x = 0; x < bmpStar.Width; x++)
{
    for (int y = 0; y < bmpStar.Height; y++)
    {
        Color pixel = bmpStar.GetPixel(x, y);
        if (pixel != Color.White)
        {
            bmpMap.SetPixel(x + xoffset, y + yoffset, pixel);
        }
    }
}

SetPixel GetPixel ( - LockBits - , , ), .

0

Source: https://habr.com/ru/post/1713636/


All Articles