Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to colour a transparent part of an image programmatically in .NET

I have an image created and saved manually as a file, which has a shape (e.g. a heart) that is transparent in the image, and the rest of the image has other colour. How to make the transparent shape with a specified colour programmatically in .NET, leaving the rest of the image untouched?

For example, provided is an image, which I want to fill transparent part with a colour.

http://www.sendspace.com/file/an53a1

like image 261
Pingpong Avatar asked Apr 10 '11 13:04

Pingpong


2 Answers

It's transparent. So draw the background first in the color you want with Graphics.Clear(), then Graphics.DrawImage() to draw the image.

like image 87
Hans Passant Avatar answered Oct 03 '22 16:10

Hans Passant


What you need is a flood fill algorithm. Please take a look at this:

http://www.codeproject.com/KB/GDI-plus/floodfillincsharp.aspx

like image 31
Marino Šimić Avatar answered Oct 03 '22 14:10

Marino Šimić