问题:复制到 Windows 剪贴板时保持图像透明度。
Windows 剪贴板本身并不支持透明度。 但是,它可以处理各种数据格式,包括 PNG,确实支持 Alpha 通道(透明度)。
为了保持透明度,请采用以下策略:
MemoryStream
和 Image.Save()
将图像转换为 PNG 流。DataObject
.DataObject
。<code class="language-csharp">public static void SetClipboardImage(Bitmap image, Bitmap imageNoTr, DataObject data) { Clipboard.Clear(); data ??= new DataObject(); //Null-conditional operator for brevity imageNoTr ??= image; //Null-conditional operator for brevity using (MemoryStream pngMemStream = new MemoryStream()) using (MemoryStream dibMemStream = new MemoryStream()) { data.SetData(DataFormats.Bitmap, true, imageNoTr); image.Save(pngMemStream, ImageFormat.Png); data.SetData("PNG", false, pngMemStream); byte[] dibData = ConvertToDib(image); dibMemStream.Write(dibData, 0, dibData.Length); data.SetData(DataFormats.Dib, false, dibMemStream); Clipboard.SetDataObject(data, true); } } public static Bitmap GetClipboardImage(DataObject retrievedData) { Bitmap clipboardimage = null; if (retrievedData.GetDataPresent("PNG", false)) { MemoryStream pngStream = retrievedData.GetData("PNG", false) as MemoryStream; using (Bitmap bm = new Bitmap(pngStream)) clipboardimage = ImageUtils.CloneImage(bm); } else if (retrievedData.GetDataPresent(DataFormats.Dib, false)) { MemoryStream dib = retrievedData.GetData(DataFormats.Dib, false) as MemoryStream; clipboardimage = dib != null ? ImageFromClipboardDib(dib.ToArray()) : null; } else if (retrievedData.GetDataPresent(DataFormats.Bitmap)) { clipboardimage = new Bitmap(retrievedData.GetData(DataFormats.Bitmap) as Image); } else if (retrievedData.GetDataPresent(typeof(Image))) { clipboardimage = new Bitmap(retrievedData.GetData(typeof(Image)) as Image); } return clipboardimage; }</code>
以上是将图像复制到 Windows 剪贴板时如何保持透明度?的详细内容。更多信息请关注PHP中文网其他相关文章!