在不同應用程式中可靠地複製貼上透明圖像
您可能遇到這樣的問題:在複製貼上影像時,透明度無法在不同程式之間保持一致。儘管Chrome可以保留透明度,但使用系統剪貼簿物件設定影像時,透明度可能會遺失。
剪貼簿限制
Windows剪貼簿本身並非原生支援影像透明度。然而,您可以利用多種資料類型來提高跨應用程式的相容性。
PNG流:最佳相容性選擇
PNG串流是保持剪貼簿影像透明度最可靠的方法。將PNG映像貼到GIMP或較新的MS Office程式中,透明度將被保留。
多種格式,更廣相容性
為了確保與更多應用程式相容,建議在剪貼簿上包含多種支援的影像格式,例如PNG、裝置無關點陣圖(DIB)和標準點陣圖。
範例程式碼
public static void SetClipboardImage(Bitmap image, Bitmap imageNoTr, DataObject data) { Clipboard.Clear(); if (data == null) data = new DataObject(); if (imageNoTr == null) imageNoTr = image; 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); // 设置PNG流(GIMP和较新MS Office兼容) Byte[] dibData = ConvertToDib(image); dibMemStream.Write(dibData, 0, dibData.Length); data.SetData(DataFormats.Dib, false, dibMemStream); // 设置DIB格式(处理错误解释透明度的应用) Clipboard.SetDataObject(data, true); } } public static Byte[] ConvertToDib(Image image) { using (Bitmap bm32b = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)) { using (Graphics gr = Graphics.FromImage(bm32b)) gr.DrawImage(image, new Rectangle(0, 0, bm32b.Width, bm32b.Height)); bm32b.RotateFlip(RotateFlipType.Rotate180FlipX); Int32 stride; Byte[] bm32bData = ImageUtils.GetImageData(bm32b, out stride); Int32 hdrSize = 0x28; Byte[] fullImage = new Byte[hdrSize + 12 + bm32bData.Length]; ArrayUtils.WriteIntToByteArray(fullImage, 0x00, 4, true, (UInt32)hdrSize); ArrayUtils.WriteIntToByteArray(fullImage, 0x04, 4, true, (UInt32)image.Width); ArrayUtils.WriteIntToByteArray(fullImage, 0x08, 4, true, (UInt32)image.Height); ArrayUtils.WriteIntToByteArray(fullImage, 0x0C, 2, true, 1); ArrayUtils.WriteIntToByteArray(fullImage, 0x0E, 2, true, 32); ArrayUtils.WriteIntToByteArray(fullImage, 0x10, 4, true, 3); ArrayUtils.WriteIntToByteArray(fullImage, 0x14, 4, true, (UInt32)bm32bData.Length); ArrayUtils.WriteIntToByteArray(fullImage, hdrSize + 0, 4, true, 0x00FF0000); ArrayUtils.WriteIntToByteArray(fullImage, hdrSize + 4, 4, true, 0x0000FF00); ArrayUtils.WriteIntToByteArray(fullImage, hdrSize + 8, 4, true, 0x000000FF); Array.Copy(bm32bData, 0, fullImage, hdrSize + 12, bm32bData.Length); return fullImage; } }
從剪貼簿擷取影像
要從剪貼簿提取影像,需要檢查各種支援的格式,優先使用PNG以確保最大可靠性:
public static Bitmap GetClipboardImage(DataObject retrievedData) { if (retrievedData.GetDataPresent("PNG", false)) { MemoryStream png_stream = retrievedData.GetData("PNG", false) as MemoryStream; if (png_stream != null) using (Bitmap bm = new Bitmap(png_stream)) return ImageUtils.CloneImage(bm); } else if (retrievedData.GetDataPresent(DataFormats.Dib, false)) { MemoryStream dib = retrievedData.GetData(DataFormats.Dib, false) as MemoryStream; if (dib != null) return ImageFromClipboardDib(dib.ToArray()); } else if (retrievedData.GetDataPresent(DataFormats.Bitmap)) return new Bitmap(retrievedData.GetData(DataFormats.Bitmap) as Image); else if (retrievedData.GetDataPresent(typeof(Image))) return new Bitmap(retrievedData.GetData(typeof(Image)) as Image); return null; } public static Bitmap ImageFromClipboardDib(Byte[] dibBytes) { if (dibBytes == null || dibBytes.Length < 4 || dibBytes.Length < (Int32)ArrayUtils.ReadIntFromByteArray(dibBytes, 0, 4, true)) return null; Int32 width = (Int32)ArrayUtils.ReadIntFromByteArray(dibBytes, 0x04, 4, true); Int32 height = (Int32)ArrayUtils.ReadIntFromByteArray(dibBytes, 0x08, 4, true); Int16 planes = (Int16)ArrayUtils.ReadIntFromByteArray(dibBytes, 0x0C, 2, true); Int16 bitCount = (Int16)ArrayUtils.ReadIntFromByteArray(dibBytes, 0x0E, 2, true); Int32 compression = (Int32)ArrayUtils.ReadIntFromByteArray(dibBytes, 0x10, 4, true); if (planes != 1 || (compression != 0 && compression != 3)) return null; PixelFormat fmt; switch (bitCount) { case 32: fmt = PixelFormat.Format32bppRgb; break; case 24: fmt = PixelFormat.Format24bppRgb; break; case 16: fmt = PixelFormat.Format16bppRgb555; break; default: return null; } Int32 stride = (((((bitCount * width) + 7) / 8) + 3) / 4) * 4; Int32 imageIndex = 40 + (compression == 3 ? 12 : 0); if (dibBytes.Length < imageIndex) return null; Byte[] image = new Byte[dibBytes.Length - imageIndex]; Array.Copy(dibBytes, imageIndex, image, 0, image.Length); Bitmap bitmap = ImageUtils.BuildImage(image, width, height, stride, fmt, null, null); // ... (ImageFromClipboardDib function continues, requires ImageUtils.BuildImage implementation) ... }
(注意:上述程式碼片段中ImageUtils.GetImageData
、ImageUtils.CloneImage
和 ImageUtils.BuildImage
需要根據實際情況自行實現。) 這些函數負責影像資料的處理和點陣圖的建立。 這部分實現取決於你使用的影像處理庫和具體的平台環境。
以上是如何在不同的應用程式中可靠地複製和貼上透明圖像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在C 中使用XML是因為它提供了結構化數據的便捷方式,尤其在配置文件、數據存儲和網絡通信中不可或缺。 1)選擇合適的庫,如TinyXML、pugixml、RapidXML,根據項目需求決定。 2)了解XML解析和生成的兩種方式:DOM適合頻繁訪問和修改,SAX適用於大文件或流數據。 3)優化性能時,TinyXML適合小文件,pugixml在內存和速度上表現好,RapidXML處理大文件優異。

C#和C 的主要區別在於內存管理、多態性實現和性能優化。 1)C#使用垃圾回收器自動管理內存,C 則需要手動管理。 2)C#通過接口和虛方法實現多態性,C 使用虛函數和純虛函數。 3)C#的性能優化依賴於結構體和並行編程,C 則通過內聯函數和多線程實現。

C 中解析XML數據可以使用DOM和SAX方法。 1)DOM解析將XML加載到內存,適合小文件,但可能佔用大量內存。 2)SAX解析基於事件驅動,適用於大文件,但無法隨機訪問。選擇合適的方法並優化代碼可提高效率。

C 在遊戲開發、嵌入式系統、金融交易和科學計算等領域中的應用廣泛,原因在於其高性能和靈活性。 1)在遊戲開發中,C 用於高效圖形渲染和實時計算。 2)嵌入式系統中,C 的內存管理和硬件控制能力使其成為首選。 3)金融交易領域,C 的高性能滿足實時計算需求。 4)科學計算中,C 的高效算法實現和數據處理能力得到充分體現。

C 沒有死,反而在許多關鍵領域蓬勃發展:1)遊戲開發,2)系統編程,3)高性能計算,4)瀏覽器和網絡應用,C 依然是主流選擇,展現了其強大的生命力和應用場景。

C#和C 的主要區別在於語法、內存管理和性能:1)C#語法現代,支持lambda和LINQ,C 保留C特性並支持模板。 2)C#自動內存管理,C 需要手動管理。 3)C 性能優於C#,但C#性能也在優化中。

在C 中處理XML數據可以使用TinyXML、Pugixml或libxml2庫。 1)解析XML文件:使用DOM或SAX方法,DOM適合小文件,SAX適合大文件。 2)生成XML文件:將數據結構轉換為XML格式並寫入文件。通過這些步驟,可以有效地管理和操作XML數據。

在C 中處理XML數據結構可以使用TinyXML或pugixml庫。 1)使用pugixml庫解析和生成XML文件。 2)處理複雜的嵌套XML元素,如書籍信息。 3)優化XML處理代碼,建議使用高效庫和流式解析。通過這些步驟,可以高效處理XML數據。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境