我正在从剪贴板粘贴图像(带透明度的PNG):
Dim oDataObj As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
Dim oImgObj As Image = oDataObj.GetData(DataFormats.Bitmap, True)
oImgObj.Save(temp_local, System.Drawing.Imaging.ImageFormat.Png)
或在C#中
IDataObject oDataObj = System.Windows.Forms.Clipboard.GetDataObject();
Image oImgObj = oDataObj.GetData(DataFormats.Bitmap, true);
oImgObj.Save(temp_local, System.Drawing.Imaging.ImageFormat.Png);
问题是图像的透明度正在丢失。
有没有办法保持图像透明度?
答案 0 :(得分:1)
位图对象无法保持透明度,这就是您失去透明度的原因
答案 1 :(得分:1)
不幸的是,剪辑板是如何工作的,它复制时没有透明度。
答案 2 :(得分:1)
我从here找到了一个出色的解决方案。我已经将代码转换为VB.NET以适应我的问题。以下代码可以解决这个问题:
Private Function GetImageFromClipboard() As Image
If Clipboard.GetDataObject() Is Nothing Then
Return Nothing
End If
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Dib) Then
Dim dib = DirectCast(Clipboard.GetData(DataFormats.Dib), System.IO.MemoryStream).ToArray()
Dim width = BitConverter.ToInt32(dib, 4)
Dim height = BitConverter.ToInt32(dib, 8)
Dim bpp = BitConverter.ToInt16(dib, 14)
If bpp = 32 Then
Dim gch = GCHandle.Alloc(dib, GCHandleType.Pinned)
Dim bmp As Bitmap = Nothing
Try
Dim ptr = New IntPtr(CLng(gch.AddrOfPinnedObject()) + 40)
bmp = New Bitmap(width, height, width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, ptr)
Return New Bitmap(bmp)
Finally
gch.Free()
If bmp IsNot Nothing Then
bmp.Dispose()
End If
End Try
End If
End If
Return If(Clipboard.ContainsImage(), Clipboard.GetImage(), Nothing)
End Function