如何使用C#制作白色背景透明的PNG图像 [英] How to make PNG image with white background transparent using C#

查看:90
本文介绍了如何使用C#制作白色背景透明的PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我正在开发一个上传用户签名的页面,规则如下:



*图片必须是PNG。

*图片大小不得超过4 MB。



现在,我想制作上传的照片如果用户没有透明,则按代码透明。



这可能吗?如果有,怎么样?



提前谢谢



我尝试过的方法:



i尝试将图像作为位图图像读取并使用MakeTransparent()Mehtod有时会成功获得具有纯白色背景的图像并且在图像有噪声时失败白色背景。



这是我的代码:



  public   static   byte  [] GetTransparentArrayFromFileWithDelete( string  pathToFile)
{
byte [] newImage = new byte [ 0 ]; error = string .Empty;
使用(位图bmp = new 位图(pathToFile))
{
Color pixel = bmp.GetPixel( 0 0 );
if (pixel.A!= 0
{
// 使myCitmap的backColor透明。
bmp.MakeTransparent(Color.Transparent);

ImageConverter converter = new ImageConverter();
newImage =( byte [])converter.ConvertTo(bmp, typeof 字节 []));
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();
}
}
尝试
{
File.Delete(pathToFile);
}
catch
{
}
return newImage;
}

解决方案

对于嘈杂的背景,失败是你应该期待的。您正在映射特定颜色,以便不渲染它。然而,其他颜色被渲染 - 因此产生噪音。



因此,由于您打开了位图,请考虑以下因素:

go通过数据并将每个像素接近白色的颜色值重置为白色,然后应用透明度。



但是,这不能解决问题在背景的浅色像素中会出现偏杂的暗像素。你可以寻找邻居,例如,两边都有光线的暗像素也可以设置为白色。



为了获得更好的抓地力*问题,在照片编辑器中打开签名。使用按颜色选择的工具并选择背景。你会看到很多未被选中的部分。您可以扩大选择窗口(颜色选择更轻松),它会更多。从来没有,你会看到漂浮物。



然后,问题是你用一些你认为合适的算法预先漂白图片的背景。





* Gimp 2是一款免费的照片编辑器,功能强大。


结果

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:

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;
        }

解决方案

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.



这篇关于如何使用C#制作白色背景透明的PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆