Click here to Skip to main content
13,056,229 members (73,636 online)
Rate this:
Please Sign up or sign in to vote.
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++)
                        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;
                        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 1:05am
lukeer 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 17-Jun-13 8:21am
Got it. Thank you.
RyanDev 2-Jan-14 16:37pm
I know it's old but please post as solution since this worked. :)
lukeer 3-Jan-14 3:41am
Glad it helped you.
And thanks.
And have a healthy new year.

1 solution

Rate this: bad
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).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.2 | Last Updated 3 Jan 2014
Copyright © CodeProject, 1999-2017
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