我正在处理一个代码,其中有一个picturebox1显示一个图像(在开始时在openFileDialog1中选择的第一个图像)和一个flowlayout面板,其中包含在openFileDialog1中选择的其他图像,包括第一个,在图片框中。现在我想要点击鼠标,我会在picturebox1中点击图片。我的问题是加载我得到第一张图片正常,当点击其他图片时,它们在picturebox1中模糊。当我再次点击第一张图片时,它也很模糊。关于它如何解决的任何想法?提前谢谢..
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; }推荐答案
您无需自己调整这些图像的大小。这就是PictureBox可以照顾的东西。只需设置每个PictureBox的 SizeMode [ ^ ]属性为 StretchImage (或缩放以保持纵横比)。 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).
更多推荐
如何在C#中用Windows单击鼠标点击模糊图像
发布评论