Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WindowsForms
I am working on a code in which there is a picturebox1 which displays an image(at the start the first image selected in openFileDialog1) and a flowlayout panel that contains other images selected in openFileDialog1 including first one ,in pictureboxes. Now I want that on mouse click I'll get the image clicked in picturebox1. My problem is on load I am getting the first picture normal and when clicked on other pictures, they are blurred in picturebox1. Also when I again click on first image, it is blurred. Any Ideas on how it can be solved ? thanks in advance..
private void button1_Click(object sender, EventArgs e)
    {
        int j;
        openFileDialog1.Multiselect = true;
 

        if ( openFileDialog1.ShowDialog() == DialogResult.OK)
        {
                   string[] file =  openFileDialog1.FileNames;
 
                   // To put the first image selected in pictureBox1
                   Image img = Image.FromFile(file[0]);
                   Image img1 = ResizeImage(img,pictureBox1.Width,pictureBox1.Height);
                   pictureBox1.Image = img1;
 

                   //to put the rest of images selected, in pictureboxes present in flowlayoutpanel 
                   // textbox1 to limit the number of images according to user.
                   for( j=0 ;j< Convert.ToInt32(textBox1.Text) ;j++)
                   {
                     try
                     {
                        PictureBox pb = new PictureBox();
                        Image loadedimage1 = Image.FromFile(file[j]);
 
                        pb.Size = new System.Drawing.Size(100, 100);
                        Image img3 = ResizeImage(loadedimage1, pb.Width, pb.Height);
                        pb.Image = img3;
 
                         flowLayoutPanel1.Controls.Add(pb);
                        pb.MouseClick += new MouseEventHandler(picturebox_load);
 
                     }
                     catch (Exception ex)
                     {
                          MessageBox.Show(". You may not have permission to read the file, or " +
                            "it may be corrupt.\n\nReported error: " + ex.Message);
                     }
                 }
 
         }
 

    }
 
    // load image on mouseclick in picturebox1
    public void picturebox_load(object sender, MouseEventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        Image img2 = pb.Image;
        Image img3 = ResizeImage(img2, pictureBox1.Width, pictureBox1.Height);
        pictureBox1.Image = img3;
 
    }
 
    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
    {
        //a holder for the result 
        Bitmap result = new Bitmap(width, height);
 
        //use a graphics object to draw the resized image into the bitmap 
        using (Graphics graphics = Graphics.FromImage(result))
        {
            //set the resize quality modes to high quality 
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //draw the image into the target bitmap 
            graphics.DrawImage(image, 0, 0, result.Width, result.Height);
        }
 
        //return the resulting bitmap 
        return result;
    } 
Posted 17-Jun-13 2:05am
mg9893244
Comments
lukeer at 17-Jun-13 8:08am
   
First thing I see is that you don't need to resize images. That's something PictureBox can take care of. Just set every PictureBox's SizeMode[^] property to StretchImage (or Zoom for keeping aspect ratio).
Member 10103134 at 17-Jun-13 8:21am
   
Got it. Thank you.
RyanDev at 2-Jan-14 16:37pm
   
I know it's old but please post as solution since this worked. :)
lukeer at 3-Jan-14 3:41am
   
Glad it helped you.
And thanks.
And have a healthy new year.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You don't need to resize those images yourself. That's something PictureBox can take care of. Just set every PictureBox's SizeMode[^] property to StretchImage (or Zoom for keeping aspect ratio).
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 Maciej Los 169
4 /\jmot 169
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Jan 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100