hi everyone, i am having a difficulty with a C# program. The program thing i don't know how to do is one button that have to draw a text to an image. I have two forms.
The first one have three buttons Open File, Edit Text and Save Image
It's code is :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Test
{
  public partial class Postcard : Form
  {
    private Image img;

    public Postcard()
    {
      InitializeComponent();
    }

    private void btnOpenImage_Click(object sender, EventArgs e)
    {
      OpenFileDialog openFileDia = new OpenFileDialog();
      if (openFileDia.ShowDialog() == DialogResult.OK)
      {

        this.img = Image.FromFile(openFileDia.FileName);
        this.butSave.Enabled = true;
        this.toolStripButton1.Enabled = true;

        try
        {
            setPicBoxImage(openFileDia.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
      }
    }


   private void setPicBoxImage(string fileName)
    {
      this.pbImage.Image = img;
      if (img != null)
      {
          img.Dispose();
      }
      pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
      img = new Bitmap(fileName);
      pbImage.Image = (Image)img;
    }

    private void butSave_Click(object sender, EventArgs e)
    {
      SaveFileDialog saveFileDia = new SaveFileDialog();
      saveFileDia.Filter = "jpeg pictures|*.jpg";
      if (saveFileDia.ShowDialog() == DialogResult.OK)
      {
        this.saveJpeg(saveFileDia.FileName, new Bitmap(this.img), 85L);
      }
    }

    private void saveJpeg(string path, Bitmap img, long quality)
    {
      // Encoder parameter for image quality
      EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);

      // Jpeg image codec
      ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

      if (jpegCodec == null)
        return;

      EncoderParameters encoderParams = new EncoderParameters(1);
      encoderParams.Param[0] = qualityParam;

      img.Save(path, jpegCodec, encoderParams);
    }

    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
      // Get image codecs for all image formats
      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

      // Find the correct image codec
      for (int i = 0; i < codecs.Length; i++)
        if (codecs[i].MimeType == mimeType)
          return codecs[i];
      return null;
    }

    private void Postcard_Load(object sender, EventArgs e)
    {
   
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        EditText myNewForm = new EditText();

        myNewForm.Show();

    }
   
  }
}

When i click the Edit Text button the second form opens . It has three listboxes with Font, Font Size and Color and one text box.
And the problem is the button Edit . When i choose the three thing from the listboxes and write the text into the textbox , i have to click the Edit button and the text with the items choosed from the listboxes have to be drawn on the image i hava already open in the previous form . The code for secon form that i have written is

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;

namespace Test
{
    public partial class EditText : Form
    {
        public EditText()
        {
            InitializeComponent();
        }

        private void AddText_Load(object sender, EventArgs e)
        {
            ArrayList fontObjList = new ArrayList();
            ArrayList colorObjList = new ArrayList();
            InstalledFontCollection InstalledFonts = new InstalledFontCollection();
            foreach (FontFamily family in InstalledFonts.Families)
            {
                try
                {
                    fontObjList.Add(new Font(family, 12));
                }
                catch
                {
                    
                }
            }

            string[] colorNames;
            colorNames = System.Enum.GetNames(typeof(KnownColor));
            TypeConverter cnvrt = TypeDescriptor.GetConverter(typeof(KnownColor));

            foreach (string colorName in colorNames)
            {
                colorObjList.Add(Color.FromKnownColor((KnownColor)cnvrt.ConvertFromString(colorName)));
            }
            listBox1.DataSource = colorObjList;
            listBox1.DisplayMember = "Name";
            listBox2.DataSource = fontObjList;
            listBox2.DisplayMember = "Name";

            int i;
            for (i = 2; i <= 25; i+=2)
            {

                listBox3.Items.Add(i);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
            //Image img = Bitmap.FromFile();
            //Graphics g = Graphics.FromImage(img);
            //g.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
            
        }

I hope somebody could help me.I would be very grateful for the help

Saving reading every line of code here. I think what you are going for is to expose the values your text dialog has as properties. and when the method that calls it returns, get those values and apply it to the image and refresh the picturebox.

Can you clarify what part you need helpo with: retrieving the text from the second form or writing the text to the image?

I need help with writing the code of the button of the second form which has to retrieve the text from the second form and draw it to the picture in the picturebox from the first form. Because this code

#
//Image img = Bitmap.FromFile();
#
//Graphics g = Graphics.FromImage(img);
#
//g.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

isn't right for my program. It's from an example how to draw text. Here

//Graphics g = Graphics.FromImage(img);

i have to get the name of the file from the picturebox, and here

//g.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

, i have to insert the font , font size , and the color from the listboxes .

The dialog modal of programming suggests that you get the data from the edit dialog back to the main form. not the other way around. your OK button should just close the form with the dialogresult property set to true. and expose the color font and text from the edit dialog as properties. Then do it all in the method that calls the edit dialog.

//code on main form.
    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        EditText myNewForm = new EditText(); 
       if(myNewForm.Showdialog() == dialogresult.OK)
       {
          Image img = picturebox.image;
         Graphics G = Graphics.FromImage(img);
         G.DrawString(myNewForm.BodyText, myNewForm.BodyFont, new SolidBrush(myNewForm.BrushColor), new PointF(0, 0));
          G.Dispose();
          picturebox.image = img;
           picturebox.Invalidate(false);
       }

}

Keep in mind this code is for example only, written here to explain the concept. It doesn't have correct names for your project, assumes changes are made that aren't shown to the original code, contains spelling and capitalization errors, etc. Give you the idea of how it should look though.

thanks for the example , i have only one question. The button in the second form should only take the data and close, am i understand write?

the 2nd form should expose the selected data as properties. when the user has set the values they wanted. the dialogresult property should be set to OK. and the form closed. Nothing else needs to happen in the 2nd from. The main form will still have a reference to the 2nd form and be able to get its data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.