【发布时间】:2014-06-19 22:24:55
【问题描述】:
我正在使用具有透明部分的现有 PNG 图像文件,在将图像保存回磁盘之前在顶部添加一些文本(使用 Graphics.DrawString())。
我还想将图像保存到剪贴板。但是,当我尝试将生成的图像粘贴到 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 = new Bitmap("D:\\2RButtons.png");图形 g = Graphics.FromImage(bmp); g.DrawString("**", new Font("Tahoma", 6), System.Drawing.Brushes.Red, new PointF(0, 0));图片框1.Image = bmp; Clipboard.SetImage(bmp); pictureBox1.Image = Clipboard.GetImage();``一切都很好,透明度得以保留。
-
..粘贴到 Paint 中也可以正常工作。 (在 win 8.1 机器上)..