2012-05-28 3 views
4

코드 :알파 채널이있는 이미지를 사용자 정의 배경색이있는 클립 보드에 복사 하시겠습니까?

private void Foo(Canvas canvas) 
{ 
    // The content is a bit larger... 
    Size size = new Size(canvas.ActualWidth * 1.1, canvas.ActualHeight * 1.2); 

    // Create a render bitmap and push the surface to it 
    RenderTargetBitmap renderBitmap = 
     new RenderTargetBitmap(
     (int)size.Width, 
     (int)size.Height, 
     96d, 
     96d, 
     PixelFormats.Pbgra32 
    ); 
    renderBitmap.Render(canvas); 

    // Then copy to clipboard 
    Clipboard.SetImage(renderBitmap); 
} 

내가 필요한 것 :

이미지에 투명 배경으로 캔버스를 렌더링, 다음

문제 (?별로 간단한 종료)가 클립 보드에 복사 :

붙여 넣기 할 때 검은 색 뒷면이있는 추한 이미지가납니다. 지상

해결 방법 1 :이 두꺼운 작동하지 않습니다, canvas의 배경이 내가 타이머를 사용해야하는 대신, 다음 renderBitmap.Render(canvas);

에 변경되지 않습니다

canvas.Background = new SolidColorBrush(Colors.White); 

번호, 제공 WPF를 사용하여 배경을 변경 한 다음 해당 타이머의 틱 이벤트에서 렌더링합니다. 작동하지만 불행히도 canvas의 내용은 크기보다 큽니다 ... 그래서 흰색 배경은 일부만을 커버 할 수 있지만 여전히 못생긴 결과입니다. (배경을 바꾸는 데 시간이 걸리는 이유는 누구나 알 수 있습니까?) 즉시 변경해야한다고 생각했습니다.

내가 잘못 했습니까? 클립 보드에서 흰색 배경의 전 투명한 이미지를 얻으려면 어떻게해야합니까?

알파 채널을 지원하지 않는 mspaint.exe에 일부 PNG 이미지를 붙여 넣으면 일부 PNG 이미지의 배경이 흰색으로 남아 있지만 다른 일부는 검정색으로 표시됩니다.

alternative color과 같은 것이 있나요? 이미지를 붙여 넣은 곳에서 알파 채널을 지원하지 않으면 배경으로 사용됩니까? 우리가 그것을 사용자 정의 할 수 있습니까? ... 배경으로이 문제가 해결 renderBitmap와 결합 할 수있는 방법이 있는지

는 지금은 흰색 내용과 다른 BitmapSource 렌더링,하지만 난 방법을 모른다

int dWidth = (int)size.Width; 
int dHeight = (int)size.Height; 
int dStride = dWidth * 4; 
byte[] pixels = new byte[dHeight * dStride]; 
for (int i = 0; i < pixels.Length; i++) 
{ 
    pixels[i] = 0xFF; 
} 
BitmapSource bg = BitmapSource.Create(
    dWidth, 
    dHeight, 
    96, 
    96, 
    PixelFormats.Pbgra32, 
    null, 
    pixels, 
    dStride 
); 
// Combine bg with renderBitmap 
+0

모든 채널 대신 알파 채널을 제거한 후 비트 맵을 저장하는 것으로 보입니다. 클립 보드의 이미지가 MS BMP입니까? –

+1

@ColeJohnson 잘 모르겠습니다. 내가 검색 한 결과는 MS 클립 보드 이미지가 투명 채널을 지원하지 않는 것 같습니다. 클립 보드로 설정하기 전에이 이미지에 흰색 배경을 추가하는 방법을 찾으려고합니다. – Byzod

+0

항상 base64로 인코딩 할 수 있으며 다른 쪽에서 base64로 디코딩 할 수 있습니다. 하지만 호환성이 깨질 수 있습니다. 이미지가있는 앱의 경우 어떻게 사용하는지 모르겠습니다. '( –

답변

2

여기 내 마지막 솔루션은이 같은 문제를 다른 사람에게 도움이 될 희망의

// Create a render bitmap and push the surface to it 
RenderTargetBitmap renderBitmap = 
    new RenderTargetBitmap(
    (int)size.Width, 
    (int)size.Height, 
    96d, 
    96d, 
    PixelFormats.Pbgra32 
); 
renderBitmap.Render(surface); 

// Create a white background render bitmap 
int dWidth = (int)size.Width; 
int dHeight = (int)size.Height; 
int dStride = dWidth * 4; 
byte[] pixels = new byte[dHeight * dStride]; 
for (int i = 0; i < pixels.Length; i++) 
{ 
    pixels[i] = 0xFF; 
} 
BitmapSource bg = BitmapSource.Create(
    dWidth, 
    dHeight, 
    96, 
    96, 
    PixelFormats.Pbgra32, 
    null, 
    pixels, 
    dStride 
); 

// Adding those two render bitmap to the same drawing visual 
DrawingVisual dv = new DrawingVisual(); 
DrawingContext dc = dv.RenderOpen(); 
dc.DrawImage(bg, new Rect(size)); 
dc.DrawImage(renderBitmap, new Rect(size)); 
dc.Close(); 

// Render the result 
RenderTargetBitmap resultBitmap = 
    new RenderTargetBitmap(
    (int)size.Width, 
    (int)size.Height, 
    96d, 
    96d, 
    PixelFormats.Pbgra32 
); 
resultBitmap.Render(dv); 

// Copy it to clipboard 
try 
{ 
    Clipboard.SetImage(resultBitmap); 
} catch { ... } 
0

내가 더 작고 더 읽기 해결책을 발견 한 나는 https://social.msdn.microsoft.com/Forums/vstudio/en-US/a6972b7f-5ccb-422d-b203-134ef9f10084/how-to-capture-entire-usercontrol-image-to-clipboard?forum=wpf에서 그것을 발견 :

// Create a render bitmap and push the surface to it 
RenderTargetBitmap renderBitmap = 
    new RenderTargetBitmap(
    (int)size.Width, 
    (int)size.Height, 
    96d, 
    96d, 
    PixelFormats.Pbgra32 
); 

// Render a white background into buffer for clipboard to avoid black background on some elements 
Rectangle vRect = new Rectangle() 
{ 
    Width = (int)size.Width, 
    Height = (int)size.Height, 
    Fill = Brushes.White, 
}; 
vRect.Arrange(new Rect(size)); 
renderBitmap.Render(vRect); 

// renderBitmap is now white, so render your object on it 
renderBitmap.Render(surface); 

// Copy it to clipboard 
try 
{ 
    Clipboard.SetImage(resultBitmap); 
} catch { ... } 
관련 문제