2012-09-05 3 views
1

캔버스에서 이미지를 분리하고 배경을 그대로 둘 수 있습니다. 결과는 아래의 ImageB.png (흰색 영역이 투명 함)와 같아야합니다. 아래에서 작성한 코드는 asp.net C# webform에 있습니다.GDI + 이미지에서 마스크 만들기

ImageA.png (배경 이미지)

ImageA(Image with background)

ImageB.png (아가씨와 이미지, 흰색 영역이 투명해야한다)

Image has been removed from background

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 
    } 

답변

1

당신은 아마 현재의 픽셀이 "배경"픽셀 인 경우 (A WriteableBitmap 사용) 각 픽셀을 통과하고 결정해야합니다 (당신은 정확한, "퍼지"일치 또는 알파 테스트하여이 작업을 수행 할 수 있습니다) - 그런 다음 흰색이나 원하는 다른 색상으로 바꿉니다.

Here's WriteableBitmap의 모든 픽셀에 액세스하는 방법

+0

예제가 있습니까? –

+0

위의 편집으로 샘플을 추가했습니다. – Ani

관련 문제