Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am loading an image into a pictureBox and few details of that image in a statusStripBar; for example,image resolution,date and time etc.,
Now i want to save both image and contents of statustripbar such that next time when the user opens the image,he/she should view both image and its details.
kindly guide me.
Posted

1 solution

Where do you get the information from, that you display on the status line?
Because, you may not be aware that all that info is probably included as part of the image already - Google for EXIF and you will see what I mean. It may be that you don't actually need to do anything to store the info!

If you do have to store it, then one way various systems use is to keep a small file in each folder that has images, with the extra information held in there for all files in the folder.


"I want the info to be stored at the bottom of the image. Just like a status bar."

Then you have two options, both very similar.
1) Write text at the bottom of your image, overwriting part of the image.
2) Create a new image, slightly larger than the original, and write the text in that.

New image is simple:
Image myPicture = Image.FromFile(@"F:\Temp\BTC.jpg");
int integerTextHeight = 20;
Bitmap b = new Bitmap(myPicture.Width, myPicture.Height + integerTextHeight);
using (Graphics g = Graphics.FromImage(myPicture))
    {
    g.DrawImage(myPicture, new Point(0,0));
    g.DrawString("My additional information at the bottom", myFont, Brushes.Black, new PointF(0, myPicture.Height));
    }
Drawing on the original is just as easy - you don't need to create the new or draw the image.
Either way, save it using the Image.Save method - it has an override to specify the file format.

"am stretching the image like this..
Image source = Image.FromFile(openFileDialog1.FileName);
Image resized = new Bitmap(source, pictureBox1.Size);
pictureBox1.Image = resized;

then also its not working"




Image source = Image.FromFile(openFileDialog1.FileName);
Image resized = new Bitmap(source, pictureBox1.Size);
using (Graphics g = Graphics.FromImage(resized))
    {
    g.DrawString("My additional information at the bottom", myFont, Brushes.White, new PointF(0, resized.Height - 20));
    }
pictureBox1.Image = resized;
Try that: text at the bottom (I used white so it showed up against my picture)



"
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPEG Files(*.jpg)|*.jpg";
Bitmap orgImg = new Bitmap(resized.Width, resized.Height);
pictureBox1.DrawToBitmap(orgImg, pictureBox1.Bounds);
if (sfd.ShowDialog() == DialogResult.OK)
    orgImg.Save(sfd.FileName);

Am saving this way, as am drawing lines on the image using pictureBox_paint.

Reply
OriginalGriff - 4 mins ago
And? What happens?

Reply
Deepurs - 1 min ago
I can save only image with lines if i use my method and image with text at the bottom of the image if i use your method. How do i save both lines and text??"


I just bolted all this together and tried it:
private void button1_Click(object sender, EventArgs e)
    {
    Font myFont = new Font("Arial", 8.0F);
    if (pictureBox1.Image == null)
        {
        Image myPicture = Image.FromFile(@"F:\Temp\BTC.jpg");
        int integerTextHeight = 20;
        Bitmap b = new Bitmap(myPicture.Width, myPicture.Height + integerTextHeight);
        using (Graphics g = Graphics.FromImage(myPicture))
            {
            g.DrawImage(myPicture, new Point(0, 0));
            g.DrawString("My additional information at the bottom", myFont, Brushes.Black, new PointF(0, myPicture.Height));
            }
        Image resized = new Bitmap(myPicture, pictureBox1.Size);
        using (Graphics g = Graphics.FromImage(resized))
            {
            g.DrawString("My additional information at the bottom", myFont, Brushes.White, new PointF(0, resized.Height - 20));
            }
        pictureBox1.Image = resized;
        }
    else
        {
        Bitmap orgImg = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(orgImg, pictureBox1.Bounds);
        orgImg.Save(@"F:\Temp\crap.bmp");
        }
    }
private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.DrawLine(Pens.Red, new Point(0, 0), new Point(pictureBox1.Width, pictureBox1.Height));
    }

On the first click, I got a stretched image, with text, and a red line.
On the second click of the button it saved a stretched picture, with text and a red line as a BMP file.
What are you doing that is different?
 
Share this answer
 
v4
Comments
Deepurs 13-May-11 3:47am    
Am getting information from first form and display those info in the second form where i load the image. So there is no way to save image along with statusstrip contents??
OriginalGriff 13-May-11 3:57am    
Of course there is: a huge number of ways!
I assumed that you already had the image on disk and wanted to add information about it in a separate file (or the EXIF information).
How do you want to store the info and image?
Deepurs 13-May-11 4:03am    
I want the info to be stored at the bottom of the image. Just like a status bar.
Deepurs 13-May-11 4:39am    
I need to stretch the image to fit the pictureBox size while loading. I tired with your code, not able add text at the bottom of the image :(
OriginalGriff 13-May-11 4:46am    
Don't use the PictureBox.SizeMode property to stretch it: When you do it applies a transform to the whole image and that affects everything you do to it, including writing text.
Instead, either draw the image yourself in the Paint event and don't use a Picture box, or use PictureBoxSizeMode.Normal and draw the image from the file into a new image (use the same code as above with an overload of DrawImage that provides a stretch). Then draw the text on that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900