Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
Dears,

I am developing a page to upload users signatures with rules that:

*Image must be PNG.
*Image size must be no more that 4 MB.

Now, i want to make the uploaded photo transparent by code if the user didn't make it transparent.

Is that is possible? if yes, How?

Thanks in advance

What I have tried:

i tried reading the image as bitmap image and i used MakeTransparent() Mehtod which sometimes succeeds with images that has pure white background and fails when the image has a noisy white background.

here is my code:

C#
public static byte[] GetTransparentArrayFromFileWithDelete(string pathToFile)
        {
            byte[] newImage = new byte[0]; error = string.Empty;
            using (Bitmap bmp = new Bitmap(pathToFile))
            {
                Color pixel = bmp.GetPixel(0, 0);                               
                if (pixel.A != 0)
                {                    
                    // Make backColor transparent for myBitmap.
                    bmp.MakeTransparent(Color.Transparent);

                    ImageConverter converter = new ImageConverter();
                    newImage = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
                    bmp.Dispose();
                }
                else
                {
                    FileStream fs = new FileStream(pathToFile, FileMode.OpenOrCreate, FileAccess.Read);
                    newImage = new byte[fs.Length];
                    fs.Read(newImage, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                }
            }
            try
            {
                File.Delete(pathToFile);
            }
            catch
            {
            }
            return newImage;
        }
Posted
Updated 5-Apr-16 3:34am
v3
Comments
Andy Lanng 5-Apr-16 9:39am    
change the whole image alpha to 0. that'll make it transparent :P

If you want to pick out the 'white' parts and change the alpha on only those to be 0, then you have to define what white is. Is it Red, Green and Blue above 250? Anything lower will be from light grey to black (and any colour in between). You can get the RGB values from the Color object returned from GetPixel()

You could try running a line-detection algorithm and make sure that the signature is all black & white(or transparent), but that might be way beyond you requirements and inclination.
There is no fully automated way to do it in native .Net, but there might be a library out there for you.
_ProgProg_ 5-Apr-16 9:41am    
can help me with code?
Sergey Alexandrovich Kryukov 5-Apr-16 10:27am    
This is wrong approach. PNG, of course, support transparency, and this is not the subset of "transparent pixels", but an alpha channel value prescribed for every individual bit, opaqueness value. If some file does not use transparency, it does not. You don't have enough information to automatically convert the file without transparency to an image with it. Moreover, it does not make sense. This is like saying "show all people with blue eyes".
—SA
_ProgProg_ 6-Apr-16 1:36am    
So, Isn't that applicable?

1 solution

The failure, for a noisy background, is what you should expect. You're mapping a particular color so that it is not rendered. Other colors, however, are rendered - hence the noise.

So, since you have the bitmap open, consider the following:
go through the data and reset the color value of each pixel that is near-white to white, then apply the transparency.

This doesn't, however, handle the problem of stray dark pixels amongst what will be your background's light-colored pixels. You could look for neighbors, a dark pixel with light on either side, for example, could be set to white, as well.

To get a better grip* on the problem, open up a signature in a photo-editor. Use the tool that selects by color and select the background. You'll see lots of unselected parts. You can widen the selection window (color selection more relaxed) and it will pick up more. Never-the-less, you'll see floaters.

The problem, then, is you pre-whitening the picture's background with some algorithm you find suitable.


*Gimp 2 is a free photo editor with a great deal of power.


 
Share this answer
 
Comments
_ProgProg_ 5-Apr-16 9:37am    
Can you help me with this algorithm?
F-ES Sitecore 5-Apr-16 9:50am    
Use GetPixel to read the colour of the pixels and if it is close to white use SetPixel to make it white. To convert the Color structure to a hex value try this, and the closer something is to ffffff the whiter it is, the closer to 000000 the blacker it is.
_ProgProg_ 5-Apr-16 10:07am    
Do i have to do that with all image pixeles? i mean do i have to loop through all the image pixels?
F-ES Sitecore 5-Apr-16 10:12am    
Yeah just have a for loop for the width and one for the height and go through them all in order.
Andy Lanng 5-Apr-16 9:41am    
5'ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900