i tried to draw many texts into image but it get error i want to make user select in image with any path and save the result in the same path with another name this is my code
open dialoge button
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
}
}
drawing button
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
}
error is
A Graphics object cannot be created from an image that has an indexed pixel format.