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