如何缩放图片框和图形 [英] How to zoom picturebox along with graphics

查看:165
本文介绍了如何缩放图片框和图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图片框和图形一起缩放.

这将缩放唯一的图像部分而不是图形部分.

I want to zoom picture box along with graphics.

this will Zoom the only image part not the graphics part.

public Image PictureBoxZoom(Image imgP, Size size)
{
    Bitmap bm = new Bitmap(imgP, Convert.ToInt32(imgP.Width * size.Width), Convert.ToInt32(imgP.Height * size.Height));
    Graphics grap = Graphics.FromImage(bm);
    grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
    return bm;
}

private void zoomSlider_Scroll(object sender, EventArgs e)
{
    if (zoomSlider.Value > 0 && img != null)
    {
        pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        pictureBox1.Image = null;
        pictureBox1.Image = PictureBoxZoom(img, new Size(zoomSlider.Value, zoomSlider.Value));
    }
}


图片的来源是:

img = Image.FromStream(openFileDialog1.OpenFile());
图片正在缩放,但是当我们在图像外部绘制矩形时,它不会与image


the source of image is:

img = Image.FromStream(openFileDialog1.OpenFile());
Picture is zooming but when we draw the rectangle outside image then it''s not zooming along with image

推荐答案

一起缩放.使用冗余和原始控件是个坏主意PictureBox,如果您除了显示静态图像外还执行其他操作.这是一些中介(控制而不是图像),仅浪费您的工作时间和一些额外的资源,却没有任何回报.如果需要缩放,可以直接在自己的自定义控件或某些现有控件(如Panel)上更好地渲染图像.请查看我过去的答案:
在图片框内添加图片 [如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在面板上捕获图形 [在mdi子表单之间画线 [在C#.net鼠标滚轮中缩放图像 [ ^ ].

另外,如果您打算放大某些位图图像(也就是说,将其放大到超过其原始大小(以像素为单位)),这也不是一个好主意,因为这样会导致质量太差.如果确实需要,请始终提供原始图像,其尺寸应等于或小于最大缩放尺寸.更好的是,如果可能的话,请诉诸矢量图形.

—SA
It''s a bad idea to use redundant and primitive control PictureBox if you do something except showing static images. This is some intermediary (control, not image) which only wastes your working time and some extra resource, without giving any benefits in return. If you need to zoom, better render image directly on your own custom control, or some existing control like Panel. Please see my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

And these answers on graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
specifically on zoom: Zoom image in C# .net mouse wheel[^].

Also, if you plan to zoom in some bitmap image (that is, enlarging it above its original size in pixels), it also would be a bad idea, because you will get prohibitively poor quality. If you really need so, always provide original image with the size equal or just a bit smaller then the maximum zoom size. Better yet, resort to vector graphics, if it''s possible.

—SA


这篇关于如何缩放图片框和图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆