我喜欢能够将图像从画布中分离出来,并单独留下背景。结果应该像下面的 ImageB.png(白色区域是透明的)。我在下面编写的代码在 asp.net C# webform 中。
ImageA.png(带背景的图像)
ImageB.png(带女士的图片,白色区域应该是透明的)
ASP.NET C# 网络表单代码。
protected void Page_Load(object sender, EventArgs e)
{
//Load image
Bitmap loadImage = new Bitmap(Server.MapPath("ImageA.png"));
//Create a canvas to work on
Bitmap canvas = new Bitmap(loadImage.Width, loadImage.Height);
// create graphic on canvas
Graphics graphicOnCanvas = Graphics.FromImage(canvas);
graphicOnCanvas.DrawImage(loadImage, 0, 0, loadImage.Width, loadImage.Height);//Draw the graphic to the canvas
//*****REMOVE IMAGE FROM BACKGROUND ?????????????????????? ********/
#region Output image on screen
MemoryStream msOut = new MemoryStream();
canvas.Save(msOut, ImageFormat.Png);//must leave as png to output as png
canvas.Dispose();//Dispose the canvas
Byte[] BitmaptoBytes = msOut.ToArray();
//convert bitmapholder to byte[] - ended
BitmaptoBytes = null;
Response.ClearHeaders();
Response.ContentType = "image/png";//to product a better jpeg we have to use this because event if using the codec to do it it still doesn't look good http://msdn.microsoft.com/en-us/library/aa479306.aspx
//disable 1 line below to prevent download from viewing
// Response.AddHeader("Content-Disposition", "attachment; filename=" + ProductImage.Substring(0, ProductImage.Length - Reverse(ProductImage).Split('.')[0].Length-1) + ".png");//changing the file name extension
Response.AddHeader("Content-Length", msOut.Length.ToString());
msOut.WriteTo(Response.OutputStream);//Sending image out
Response.End();
loadImage.Dispose();
#endregion
}