0

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.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Amr
  • 100
  • 2
  • 10
  • do a simple google search on the exact error `C# A Graphics object cannot be created from an image that has an indexed pixel format` and it will have tons of examples of how to correct your issue.. good luck – MethodMan Dec 05 '16 at 20:46
  • 1
    Possible duplicate of [Graphics on indexed image](http://stackoverflow.com/questions/17313285/graphics-on-indexed-image) – MarkusEgle Dec 05 '16 at 20:47
  • @MethodMan if u can help me if not thanks :) – Amr Dec 05 '16 at 20:55
  • @MarkusEgle how the code will look like so ? – Amr Dec 05 '16 at 20:55
  • Do your own research, see the links in http://stackoverflow.com/q/10321128/3090544 – MarkusEgle Dec 05 '16 at 20:57
  • i thing i solved the first problem but i have now another error A generic error occurred in GDI+. – Amr Dec 05 '16 at 21:06

1 Answers1

0

Your original image has a palette and is not a 24/32bit RGB image. You cannot create a Graphics object for that since you cannot do all the things GDI+ can on it easily.

You can create a new Bitmap with 24/32bit RGB format, draw your existing image on that and then draw whatever you want on top of it and save.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74