如何在C#中用Windows单击鼠标点击模糊图像

编程入门 行业动态 更新时间:2024-10-26 06:26:10
本文介绍了如何在C#中用Windows单击鼠标点击模糊图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理一个代码,其中有一个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单击鼠标点击模糊图像

本文发布于:2023-11-07 01:10:14,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中用   单击   鼠标点击   图像   模糊

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!