Hi,

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#.

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();
                

            }

        }

I tired to set the mybitmap DPI to 300. but this not get reflecting.

can any one help on this.

My objective : like to place a text on the image where user likes.

You can get better control of how text is placed if you use the DrawString that has StringFormat as a parameter.

I am new to C# (i am learning guy)and I not get u. Please explain in brief

I tried with below code but not get as expected

StringFormat str = new StringFormat();
                            str.Alignment = StringAlignment.Center;
                            PointF point = new PointF(rect.X, rect.Y);//this is the point of rectangle drawn

                            e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor),point, str);
                            g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor),point, str);

is there is any thing needs to do in save tool tip?

or

is there is any thing with DPI. Picturebox DPI is always 96. I can't able to edit it. It is in read only mode. How can I edit this DPI.

Hi,

When I analyzed your code, the first if(...) statement draw a label both in the picture box (e.Graphics.DrawString) and the image (g.DrawString). I think you're missing some g.DrawString(...) in your else if{...}else{...} structure. You might not able to see the string because it is not drawn in the bitmap image. Try to put those and it might solve the problem.

Cheers,
Joel

initially i am tying with top left alignment. so my else if and else statement never triggered.

Hi,

You can try to debug your application by drawing the actual image on your picturebox's paint event, so you would see where the actual label are being painted:

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 ) {
						//Modified line starts here...
						StringFormat sf = new StringFormat();
						sf.LineAlignment = StringAlignment.Near; //Top
						sf.Alignment = StringAlignment.Near;     //Left
						e.Graphics.DrawString( label1.Text, label1.Font, new SolidBrush( label1.ForeColor ), rect, sf );

						//You may want to move this on your actual Save event
						g.DrawString( label1.Text, label1.Font, new SolidBrush( Color.Black ), rect, sf );
						
						//Uncomment this line to show the actual bitmap
						//e.Graphics.DrawImageUnscaled( mybitmap, 0, 0 );
						//Modified line ends here.

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

				}

			}

		}

Be aware though that each paint event triggered would mean that g.DrawString() will be called, hence you'll see many painted text across your image (depending where your mouse starts to drag).

Also try to use:

pictureBox1.Invalidate()

instead of

this.Invalidate()

for you to see the real-time effect.

Cheers,
Joel

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

//in save tool tip
if (SaveFD1.FileName[++r] == 'j')
                        {
                            bac = pictureBox1.Image;
                            //bac = mybitmap;//if i use this no problem
                            using (Graphics g = Graphics.FromImage(bac))
                            {
                               g.DrawImage (mybitmap,0,0);
                             
                            }
                            bac.Save(filename, ImageFormat.Jpeg);

the problem occurs...i tried to set the bitmap DPI(mybitmap) to 300, 300 but this not reflect in the image (bac) how to set the DPI of image or picturebox.

If you only want to set the DPI in your "bac" image, you can do this before saving:

bac.SetResolution( 300, 300 );
bac.Save(filename, ImageFormat.Jpeg);

Cheers,
Joel

can't able to set resolution for bac and picturebox not having "setresolution". Iam using C# 2005 expression edition

There's no SetResolution() method in the PictureBox control. As the computer screen display default DPI is set to 72, I believe you'll only see the actual change when you print the image base on the DPI you set in the bitmap using Bitmap.SetResolution(dpiX, dpiY)...

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.Image = image;
pictureBox1.Size = image.Size;

piturebox paint event

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

in Save tool tip

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).

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();

At last i got what i expected... u can set the picturebox or bit map to the original image size

Size = sz;
sz.width = image.width; sz.height = image.height;
picturebox.size = sz;

or

at picturerbox mouse event

 mybitmap = new Bitmap(sz.Width, sz.Height);

or

u can reduce the image size to the size u like and save the image to his original size.

public int a, b, c, d, f, k, n;
        Bitmap mybitmap, my;
        string file = "";
        Size sz;
        Size sz1;
        Image image;
        Rectangle rect;
        List<Rectangle> rectangles = new List<Rectangle>();
        Graphics g;

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;
                 image = Image.FromFile(file);
                 pictureBox1.Image = image;
                 k = image.Width; n = image.Height;
                 Bitmap my1 = new Bitmap(file);
                 sz1.Width = 800; sz1.Height = 600;//size u like
                 resizeimage(my1, sz1);
                label1.Text = "";
                textBox1.Visible = false;
                button1.Visible = false;
                button2.Visible = false;
                rectangles.Clear();
               
                
                

            }

        }

private void resizeimage(Bitmap my, Size sz1)
    {
        double ratio = 0d;
        double myThumbWidth = 0d;
        double myThumbHeight = 0d;
        int x = 0;
        int y = 0;
        Bitmap bp;

/*Use this when saving the file(if ((Convert.ToDouble(sz1.Width)/my.Width) > (Convert.ToDouble(sz1.Height)/my.Height))*/

       if ((my.Width / Convert.ToDouble(sz1.Width)) > (my.Height /
        Convert.ToDouble(sz1.Height)))
            ratio = Convert.ToDouble(my.Width) / Convert.ToDouble(sz1.Width);
        else
            ratio = Convert.ToDouble(my.Height) / Convert.ToDouble(sz1.Height);
        myThumbHeight = Math.Ceiling(my.Height / ratio);
        myThumbWidth = Math.Ceiling(my.Width / ratio);

        Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
        bp = new Bitmap(sz1.Width, sz1.Height);
        x = (sz1.Width - thumbSize.Width) / 2;
        y = (sz1.Height - thumbSize.Height);
        System.Drawing.Graphics g = Graphics.FromImage(bp);
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
        g.DrawImage(my, rect, 0, 0, my.Width, my.Height, GraphicsUnit.Pixel);
        my = bp;
        pictureBox1.Image = bp;
        sz = my.Size;
        a = sz.Width; b = sz.Height;

    }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
                if (mybitmap == null)
                {
                    mybitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);

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

            
             
        }

       
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
                button1.Visible = true;
                button2.Visible = true;

                if (mybitmap == null)
                {
                    return;
                }

                    
                    using (g = Graphics.FromImage(mybitmap))
                    {
                        if (rectangles.Count == 0)
                            g.Clear(Color.Transparent);
                        Pen pen = new Pen(Color.Black, 2);
                        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                        foreach (Rectangle r in rectangles)
                        {
                            e.Graphics.DrawRectangle(pen, r);
                            label1.Top = rect.Top; label1.Left = rect.Left; label1.Width = rect.Width;
                            label1.Height = rect.Height;
                            e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), r);
                            g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect);
                            g.DrawRectangle(pen, r);

                }

}
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
                        
            SaveFileDialog SaveFD1 = new SaveFileDialog();
            SaveFD1.FileName = "";
            SaveFD1.InitialDirectory = "C";
            SaveFD1.Title = "save file Name";
            SaveFD1.Filter = "JPG|*.jpg|Bmp|*.bmp";


            if (mybitmap != null)
            {

                if (SaveFD1.ShowDialog() == DialogResult.OK)
                {
                    if (SaveFD1.FileName == "")
                        return;
                    else
                    {

                        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')
                        {
                            if(!(rectangles.Count == 0))
                            {
                                using (g = Graphics.FromImage(pictureBox1.Image))
                                {
                                    foreach (Rectangle r1 in rectangles)
                                    {
                                       //mybitmap.SetResolution(300, 300);
                                       // b = a - r1.X; c = b - r1.Y;
                                        g.DrawImage(mybitmap,0,0);                                        
                                    }

                                }
                            }
                            else
                            {
                                
                                using (g = Graphics.FromImage(pictureBox1.Image))
                                {

                                    g.DrawImage((Image)mybitmap,0,0);
                                 }
                            }
                            sz1.Width = (k); sz1.Height = (n);
                            resizeimage1(pictureBox1.Image, sz1);\\call resizeimage (only if statement changes)
                            pictureBox1.Image.Save(filename, ImageFormat.Jpeg);
                            rectangles.Clear();
                            pictureBox1.Invalidate();
                        }

                                             
                      }

                        filename.Close();
                    }
                }
                button1.Visible = false;
                button2.Visible = false;

            }

        }
private void button1_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
            label1.Font = fontDialog1.Font;
            Invalidate();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            label1.ForeColor = colorDialog1.Color;
            Invalidate();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text;
         
        }

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            

                
                    if (e.Button == MouseButtons.Left)
                    {
                        rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
                        rectangles.Add(rect);
                        pictureBox1.Invalidate();

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