1

我喜欢能够将图像从画布中分离出来,并单独留下背景。结果应该像下面的 ImageB.png(白色区域是透明的)。我在下面编写的代码在 asp.net C# webform 中。

ImageA.png(带背景的图像)

ImageA(带背景的图像)

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

1 回答 1

1

您可能需要检查每个像素(使用WriteableBitmap)并确定当前像素是否为“背景”像素(您可以通过精确的“模糊”匹配或 alpha 测试来做到这一点) - 然后将其替换为白色或您想要的任何其他颜色。

下面介绍如何访问 WriteableBitmap 中的所有像素。

于 2012-09-05T17:43:48.797 回答