我希望能够从画布中分割图像并保留背景。结果应该像下面的ImageB.png(白色区域是透明的)。我在下面写的代码是在asp.net C#webform中。
ImageA.png (带背景的图片)
ImageB.png (与女士合影,白色区域应该是透明的)
ASP.NET C#webform代码。
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
}
答案 0 :(得分:1)
您可能需要浏览每个像素(使用WriteableBitmap)并确定当前像素是否为“背景”像素(您可以使用精确的“模糊”匹配或alpha测试来执行此操作) - 然后用白色或任何其他颜色替换它。
Here's如何访问WriteableBitmap中的所有像素。