Transparent image and background color

1

I have the following code:

FileStream fs = new FileStream(@"\path\imagem1.png", FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();

Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);

graphics.DrawString("Meu texto", new Font("Arial", 50), Brushes.White, 0, 0);
b.Save(@"\path\resultado.png", image.RawFormat);

image.Dispose();
b.Dispose();

Image1:

Yes,thereisanimagethere!Butitdoesnothavebackgroundcolor,italsohasagreatleveloftransparency.

TheproblemisthatIwouldliketoputabackgroundonit,sotheresultwouldbe:

I tried to draw a red rectangle, but it overlays the image.

    
asked by anonymous 10.11.2015 / 19:05

1 answer

1

Well, the following code solved the problem:

FileStream fs = new FileStream(@"\path\imagem1.png", FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();

var imageHeight = image.Height;
var imageWidth = image.Width;

Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);
graphics.Clear(Color.FromArgb(255, Color.Red));

graphics.DrawImage(image, 0, 0, imageWidth, imageHeight);

graphics.DrawString("Meu texto", new Font("Arial", 50), Brushes.White, 0, 0);
b.Save(@"\path\resultado.png", image.RawFormat);

image.Dispose();
b.Dispose();
    
10.11.2015 / 20:28