我正在使用一個現有的PNG圖像文件,它具有透明部分,在將圖像保存回磁盤之前在頂部添加一些文本(使用Graphics.DrawString())。C#複製透明PNG到剪貼板
我想也保存圖像到剪貼板。但是,當我嘗試將生成的圖像粘貼到MS Paint中時,透明區域呈淺灰色。然而,保存的文件保持正確的透明度。
這是我目前有:
//reads file into an System.Drawing.Image
FileStream fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();
//add text to image via System.Drawing.Graphics
Bitmap myBitmap = new Bitmap(image);
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString(textToAdd, new Font("Tahoma", 14), System.Drawing.Brushes.Black, new PointF(0, 0));
//save modified image back to disk (transparency works)
myBitmap.Save(fileLocation, System.Drawing.Imaging.ImageFormat.Png);
//Copy to clipboard (transparent areas are now gray)
System.Windows.Forms.Clipboard.SetImage(myBitmap);
這應該工作。它在這裏工作!位圖bmp =新位圖(「D:\\ 2RButtons.png」); Graphics g = Graphics.FromImage(bmp); g.DrawString(「**」,new Font(「Tahoma」,6),System.Drawing.Brushes.Red,new PointF(0,0)); pictureBox1.Image = bmp; Clipboard.SetImage(bmp); pictureBox1.Image = Clipboard.GetImage();''一切都很好,透明度被保留。 – TaW
..保存到Paint中也可以正常工作。 (在贏8.1機器上).. – TaW