Click here to Skip to main content
15,881,791 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
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..
C#
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
Comments
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).
mg9893 17-Jun-13 8:21am    
Got it. Thank you.
ZurdoDev 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

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).
 
Share this answer
 

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