开发者

Create a PNG having JPG and AlphaChannel in C#

How can I create a PNG file if I have two separated JPG files with the image and the alpha chann开发者_开发知识库el?

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜