Skip to content

Text Alignment problem in picturebox...

Nivas Sadashivam

Asked May 28, 2010, 12:24 AM EDT1 answer
Share
LinkedInX
Hi,

I like to place a  transparent text on the image and also like to save the work. for that, I have the picture box in which i draw the rectangle (using graphics paint) and inside that rectangle I typed some name using label box and stored the work, have done

When I opened the saved Image the position of text I typed in the label box not in the position where I try to place (on screen). It's saving the text in some other position.

How to co-ordinate the resolution using c#.
Code
C#
Image bac;

Bitmap myBitmap;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

{

            if (mybitmap == null)

            {

                mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());

 

            }

 

            rect = new Rectangle(e.X, e.Y, 0, 0);

             this.Invalidate();

 

}

 

 

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

{

 

 

               switch (e.Button)

      {

                case MouseButtons.Left:

           {

                    rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);

                    this.Invalidate();

 

                break;

            }

 

       }

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)

{

 

            if (mybitmap == null)

            {

                return;

            }

            using (Pen pen = new Pen(Color.Red, 2))

            {

                using (Graphics g = Graphics.FromImage(mybitmap))

                {

                e.Graphics.DrawRectangle(pen, rect);

 

                 if (label1.TextAlign == ContentAlignment.TopLeft)

                    {

                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds); 

                        g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);

mybitmap.PixelFormat);

                    }

                    else if (label1.TextAlign == ContentAlignment.TopCenter)

                    {

                        SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);

                        float left = ((float)this.Width + label1.Left) / 2 - size.Width / 2;

                        RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);

                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);

 

                    }

                    else

                    {

                        SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);

                        float left = (float)label1.Width - size.Width + label1.Left;

                        RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);

                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);

                    }

 

               }

 

        }

 

}

 

private void saveToolStripMenuItem_Click(object sender, EventArgs e)

{

 

 

            if (mybitmap != null)

            {

                SaveFileDialog SaveFD1 = new SaveFileDialog();

                SaveFD1.FileName = "";

                SaveFD1.InitialDirectory = "C";

                SaveFD1.Title = "save file Name";

                SaveFD1.Filter = "JPG|*.jpg|Bmp|*.bmp";

                 if (SaveFD1.ShowDialog() == DialogResult.OK)

                {

 

                    System.IO.Stream filename = (System.IO.FileStream)SaveFD1.OpenFile();

 

                    int r = SaveFD1.FileName.Length;

                    for (int r1 = 0; r1<=r;)

                    {

                        if (SaveFD1.FileName[r1] != '.')

                            r1++;

                        else

                        {

                            r = r1;

                            break;

                        }

                    }

 

                       if (SaveFD1.FileName[++r] == 'j')

                        {

                            using (Graphics g = Graphics.FromImage(bac))

                        {

                           g.DrawImage(mybitmap, 0, 0);

                        }

                        bac.Save(filename, ImageFormat.Jpeg);

                        }

                        else if (SaveFD1.FileName[r] == 'b')

                        {

                            using (Graphics g = Graphics.FromImage(bac))

                            {

                                g.DrawImage(mybitmap, 0, 0);

                            }

                            bac.Save(filename, ImageFormat.Jpeg);

                        }

                        else

                        {

                            using (Graphics g = Graphics.FromImage(bac))

                            {

                                g.DrawImage(mybitmap, 0, 0);

                            }

                            bac.Save(filename, ImageFormat.Png);

                        }

 

                    filename.Close();

 

                }

            }

        }

 

private void openToolStripMenuItem_Click(object sender, EventArgs e)

{

 

            OpenFD.FileName = "";

            OpenFD.Title = "open image";

            OpenFD.InitialDirectory = "C";

            OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";

            if (OpenFD.ShowDialog() == DialogResult.OK)

            {

                file = OpenFD.FileName;

 

                bac = Image.FromFile(file);

                pictureBox1.Image = bac;

                pictureBox1.Invalidate();

 

 

            }

 

        }


If not use the image (loaded from drive) and use only the bitmap on the picture box the rectangle and text alignment (with same font size and same position) are perfect. if i use the image the problem occurs

Code
Code
//in save tool tip

if (SaveFD1.FileName[++r] == 'j')

                        {

                            bac = pictureBox1.Image;

                            //bac = mybitmap;//no problem if I use this

                            using (Graphics g = Graphics.FromImage(bac))

                            {

                               g.DrawImage (mybitmap,0,0);

 

                            }

                            bac.Save(filename, ImageFormat.Jpeg);


Please tell me where i am going wrong...

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    I got the output as i expected. I Place the picture box size with original image size and its works as expected (initially tried with picture box size mode as "zoom") .

    replace or add the below code in the previous once at appropriate areas.

    In open tool strip menu
    pictureBox1.Size = image.Size;
    pictureBox1.Image = image;

    piturebox paint event
    Code
    //for  label1.TextAlign == ContentAlignment.TopLeft)
    g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect);
        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect);


    in Save tool tip

    Code
    bac = pictureBox1.Image;
    
     
    
    using (Graphics g = Graphics.FromImage(bac))
    
       {
    
    g.DrawImage(mybitmap,0,0);
    
        }
    
    bac.Save(filename, ImageFormat.Jpeg);



    Or


    you can add the original image width and height to bitmap (without changing the size of the picture box).

    TypeScript
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    
            {
    
                if (mybitmap == null)
    
                {
    
                    mybitmap = new Bitmap(sz.Width, sz.Height);
    
     
    
                }
    
                rect = new Rectangle(e.X, e.Y, 0, 0);
    
                this.Invalidate();
    0