Create a PNG having JPG and AlphaChannel in C#

*爱你&永不变心* 提交于 2019-12-12 20:09:31

问题


How can I create a PNG file if I have two separated JPG files with the image and the alpha channel?

JPG Image + JPG Mask --> PNG with transparency

I've tried WPF solutions:

image.OpacityMask = new ImageBrush ( new BitmapImage ( uriMask ) );

and System.Drawing.

Image bmpMask = Image.FromFile ( maskFilePath );
gfxNewImage.SetClip (  Graphics.FromImage (bmpMask) );

but I have problems with both (black background, or full black)

Any idea or example?

ThankYou


回答1:


Ok Let's assume that you have a JPEG Picute and a Png Picture with Alpha Channel . You should do this than ,and it will work only if these 2 Images are the Same Size.

Bitmap jpegFile = new Bitmap("JpegFile.jpg");
Bitmap alphaFile = new Bitmap("Png.jpg");

for(int i=0;i<jpegFile.Width;i++)
{
   for(int j=0;j<jpegFile.Height;j++)
    {
       Color withAlpha = jpegFile.GetPixel(i,j);
           withAlpha.A = alphaFile.GetPixel(i,j).A;
       jpegFile.SetPixel(withAlpha);
    }
}



回答2:


try this parameters for Grafics object:

using (var g = Graphics.FromImage(bmpMask))
{
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(source, 0, 0, width, height);
}


来源:https://stackoverflow.com/questions/7360976/create-a-png-having-jpg-and-alphachannel-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!