-1

I'm trying to add a small bitmap on a larger bitmap but the transparency of the small one is not transparent ! It's like if the opacity was 50%.

Here is the large image : enter image description here

And the small image :

enter image description here

Then the result :

enter image description here

I don't understand why the background of the small image is not transparent but "pink".

Here is my code :

Bitmap baseImage;
Bitmap overlayImage;

baseImage = (Bitmap)Image.FromFile(Path.Combine(Directory.GetCurrentDirectory(), @"Assets", "Img", "background_blue.jpg"));

overlayImage = (Bitmap)Image.FromFile(Path.Combine(Directory.GetCurrentDirectory(), @"Assets", "Img", "circle_red.png"));

var finalImage = new Bitmap(overlayImage.Width, overlayImage.Height);
var graphics = Graphics.FromImage(finalImage);
graphics.CompositingMode = CompositingMode.SourceOver;

graphics.DrawImage(baseImage, 0, 0);
graphics.DrawImage(overlayImage, 0, 0);


//save the final composite image to disk
finalImage.Save(Path.Combine(Directory.GetCurrentDirectory(), @"Assets", "Img", "result.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

Could you help me to understand what's going on, I'm completely lost ! :D

Adz
  • 281
  • 3
  • 16
  • [Is any of this helpful](https://stackoverflow.com/questions/10658994/using-graphics-drawimage-to-draw-image-with-transparency-alpha-channel)? – 15ee8f99-57ff-4f92-890c-b56153 Jul 29 '19 at 17:50
  • Possible duplicate of [Using Graphics.DrawImage() to Draw Image with Transparency/Alpha Channel](https://stackoverflow.com/questions/10658994/using-graphics-drawimage-to-draw-image-with-transparency-alpha-channel) –  Jul 29 '19 at 17:51
  • 1
    I have had this happen with other .NET libraries with jpegs. There is not a true transparent option in that file format, so it defaults it to that pink color when you move it to Bitmap. William's answer should work, as it makes "...the default transparent color transparent for myBitmap." (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.maketransparent?view=netframework-4.8) – Sam-Graham Jul 29 '19 at 17:55
  • 2
    Code works as posted. Save it as a png for better results, but I can't duplicate the semi-transparenciness that you have with the images you supplied. – LarsTech Jul 29 '19 at 17:57
  • 1
    Sorry, but I tried this out and it simply worked fine. I did add a `using` block around the graphics part though; you should always dispose your `IDisposable` objects. Including the bitmaps. – Nyerguds Jul 29 '19 at 18:21
  • I have tried all the solutions but nothing works. It seems to be a problem either with the file format or my VS Project. Indeed, if my circle_red image is not red but black it works (and only if I create a new file with a black circle, not if I color the red circle in black...). Moreover my VS Project is a Core Net API, I need to create an API which generates some images, and also I work on Mac, not Windows... Can it be the issue ? – Adz Jul 30 '19 at 08:05
  • FYI I tried with a PNG downloaded on flaticon.com with red color and it works ! So the problem may come from the file format, but I don't understand why it works for you – Adz Jul 30 '19 at 08:12
  • "Moreover my VS Project is a Core Net API" That's something you should mention _at the start_. Preferably as tag on the question. If this issue can indeed occur on other platforms, and that's a problem for you, you should probably look for WPF-specific alternatives instead of using `system.drawing`. – Nyerguds Aug 01 '19 at 07:58

2 Answers2

0

Try to use Bitmap.MakeTransparent() on your overlayImage.

https://learn.microsoft.com/fr-fr/dotnet/api/system.drawing.bitmap.maketransparent?view=netframework-4.8

William
  • 249
  • 3
  • 10
  • 1
    If just calling Bitmap.MakeTransparent() doesn't work, you can pass in the color of the top pixel. ``` // Get the color of a background pixel. Color backColor = myBitmap.GetPixel(1, 1); // Make backColor transparent for myBitmap. myBitmap.MakeTransparent(backColor); ``` also from https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.maketransparent?view=netframework-4.8#System_Drawing_Bitmap_MakeTransparent_System_Drawing_Color_. – Sam-Graham Jul 29 '19 at 18:00
  • Have you tested it ? I can't right now, but since the pink is just the default color for transparent pixel and not present on the original image, I thought that Bitmap.MakeTransparent() was working – William Jul 29 '19 at 18:06
  • I haven't yet, but if I remember right it sometimes get's wonky even though it is the default, hopefully your answer works as posted. I think there is a good chance it will. – Sam-Graham Jul 29 '19 at 18:09
  • Thanks Sam-Graham for your help about MakeTransparent, I didn't know it works like that ! But I still have the issue, it seems to come from my file. As I said in my previous comment, if the circle is black but not red it works quite well. That's strange... – Adz Jul 30 '19 at 08:07
0

I have copied my project from Mac to Windows, then I've just run it and it worked. So the problem is just with VS on Mac.

Adz
  • 281
  • 3
  • 16