我试图将许多文本绘制到图像中但是它会出错我想让用户在图像中选择任何路径并将结果保存在同一路径中,而另一个名称这是我的代码
打开拨号按钮
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of1 = new OpenFileDialog();
of1.Filter = "JPEG|*.jpg|PNG|*.png|GIF|*.gif|BMP|*.bmp";
if ((of1.ShowDialog())==System.Windows.Forms.DialogResult.OK) {
imagepath = of1.FileName; //image path
}
}
绘图按钮
private void button2_Click(object sender, EventArgs e)
{
string firstText = "G";
string secondText = "X";
string thirdText = "H";
string fourthText = "Z";
string fifthText = "Y";
string sixthText = "K";
string seventhText = "F";
string eighthText = "E";
PointF GLocation = new PointF(60f, 15f);
PointF XLocation = new PointF(145f, 15f);
PointF HLocation = new PointF(200f, 15f);
PointF ZLocation = new PointF(188f, 110f);
PointF YLocation = new PointF(7f, 100f);
PointF KLocation = new PointF(145f, 110f);
PointF FLocation = new PointF(213f, 96f);
PointF ELocation = new PointF(140f, 138f);
string imageFilePath = imagepath;
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(firstText, arialFont, Brushes.Blue, GLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, XLocation);
graphics.DrawString(thirdText, arialFont, Brushes.Blue, HLocation);
graphics.DrawString(fourthText, arialFont, Brushes.Red, ZLocation);
graphics.DrawString(fifthText, arialFont, Brushes.Blue, YLocation);
graphics.DrawString(sixthText, arialFont, Brushes.Red, KLocation);
graphics.DrawString(seventhText, arialFont, Brushes.Blue, FLocation);
graphics.DrawString(eighthText, arialFont, Brushes.Red, ELocation);
}
}
bitmap.Save(imageFilePath);//save the image file
}
错误是
无法从具有索引像素格式的图像创建Graphics对象。
答案 0 :(得分:0)
您的原始图像具有调色板,而不是24/32位RGB图像。您无法为此创建Graphics
对象,因为您无法轻松完成GDI +所能做的所有事情。
您可以使用24/32位RGB格式创建新的Bitmap
,在其上绘制现有图像,然后在其上绘制任意内容并保存。