2016-12-05 5 views
0

Ich habe versucht, viele Texte in Bild zu zeichnen, aber es bekomme Fehler Ich möchte Benutzer in Bild mit einem beliebigen Pfad auswählen und speichern Sie das Ergebnis im selben Pfad mit einem anderen Namen das ist mein CodeWie zeichne Text in Bild mit C#

offen dialoge Taste

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 
    } 
} 

Zeichnung Taste

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 
} 

Fehler ist

Ein Grafikobjekt kann nicht aus einem Bild mit einem indizierten Pixelformat erstellt werden.

+0

eine einfache Google-Suche nach dem genauen Fehler 'C# Ein Grafik-Objekt kann nicht aus einem Bild, das ein indiziertes Pixel-Format hat erstellt werden und es wird Tonnen von Beispielen haben, wie Sie Ihr Problem zu korrigieren .. viel Glück – MethodMan

+1

Mögliche Duplikat von [Grafiken auf indiziertem Bild] (http://Stackoverflow.com/questions/17313285/graphics-on-indexed-image) – MarkusEgle

+0

@MethodMan wenn du mir helfen kannst wenn nicht danke :) – Amr

Antwort

0

Ihr Originalbild hat eine Palette und ist kein 24/32-Bit-RGB-Bild. Sie können kein Objekt Graphics für das Objekt erstellen, da Sie nicht alle Dinge tun können, die GDI + problemlos ausführen kann.

Sie können eine neue Bitmap mit 24/32bit RGB-Format erstellen, zeichnen Sie Ihr vorhandenes Bild auf und dann zeichnen Sie, was Sie wollen darüber und speichern.

+0

Wie sieht der Code aus? – Amr