C#Winforms大小PictureBox不失真 [英] C# Winforms Size PictureBox Without Distortion

查看:379
本文介绍了C#Winforms大小PictureBox不失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个C#WinForms应用程序,需要一次显示一系列图像.原始图像均为512w x 768h,我需要将它们拉伸以在保持宽高比(1到1.5)的同时,以计算机屏幕允许的最大高度显示.我将PictureBox停靠在固定高度的窗体的右侧.

如何计算宽度?我不能简单地将宽度乘以原始高度和新高度的倍数.当我这样做时,图像在某些计算机上可以正常显示,但在其他计算机上的高度或宽度会失真.

这是我现在正在做的事情:

Hi Guys,

I have a C# WinForms application where I need to display a series of images one at a time. The original images are all 512w x 768h and I need to stretch them to display at the maximum height the computer screen will allow while maintaining the aspect ratio (1 to 1.5). I Dock the PictureBox to the right side of the form which fixes the height.

How do I compute the width? I can''t simply multiply the width by a factor of original height and new height. When I do this, images will display fine on some computers but will be distorted in height or width on others.

Here''s what I''m doing now:

int newHeight = pbImage.ClientRectangle.Height;
double multiplier = (newHeight / 768D);
int newWidth = ((int)Math.Ceiling(512 * multiplier));



就像我说的那样,这适用于部分而非全部计算机.有解决方案吗?

谢谢....



As I said, this works on some but not all computers. Is there a solution?

Thanks....

推荐答案

这一切都是关于纵横比:
http://en.wikipedia.org/wiki/Aspect_ratio [
This is all about aspect ratio:
http://en.wikipedia.org/wiki/Aspect_ratio[^].

This is hard-coded aspect ratio, so this code is not useful at all. You need to use:
//having
PictireBox myPictireBox = //...

//...

//you can find the required aspect ratio using
int width = myPictireBox.Image.Width;
int height = myPictireBox.Image.Height;

//and from here, you can adjust myPictireBox.Width or myPictireBox.Height accordingly



请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.picturebox.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.drawing. image.aspx [^ ].

这听起来像对类PictureBox的合法使用,但也请考虑不使用它.此类经常被滥用,并且,如果您有任何机会决定获得稍微更复杂的渲染或行为,则该类将变得麻烦而不是提供帮助.立即在您当前用作图片框父级的控件表面上渲染图像和图形要容易得多,因此图片框只是您真正不需要的傻瓜中间人.请查看我过去有关相关主题的答案:

如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [在图片框内添加图片 [在mdi子表单之间画线 [在面板上捕获图形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [



Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^].

This sounds like a legitimate use of the class PictureBox, but also consider not using it. This class is often heavily abuses, and, if by any chance you decide to get just a bit more complex rendering or behavior, this class will be turned a hassle rather then help. It could be much easier to render your images and graphics immediately on the surface of the control you presently use as a parent of your picture box, so a picture box is just a dumb middleman you don''t really need. Please see my past answers on related topics:

How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^];

and also:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Good luck,

—SA


这篇关于C#Winforms大小PictureBox不失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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