如何更改图像位图尺寸? [英] how to change image bitmap dimensions?
本文介绍了如何更改图像位图尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
位图img; // 当前方法之外。
private void Form1_Load( object sender ,EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
img = new 位图(file.FullName);
if (img.Height > = pictureBox1.Height)
{
// 如何更改img维度?
// 我想设置加载图像的高度或宽度,使其正确放入我的图片框。
// 如果图像模式是垂直的,我的图片框必须有高度>宽度。 img的高度将改变图片盒的高度。
}
pictureBox1.Image = img;
}
解决方案
我几年前在博客上发现了这个,但找不到原版。这是我修改过的博客版本。这是C ++,但原则是相同的,将其转换为C#应该不会太困难:
// 获取目标坐标
:: GetClientRect(hWnd,& rectWindow);
destRect.X = static_cast< real>(rectWindow.left);
destRect.Y = static_cast< real>(rectWindow.top);
destRect.Width = static_cast< real>((rectWindow.right - rectWindow.left));
destRect.Height = static_cast< real>((rectWindow.bottom - rectWindow.top));
/ *
*在调整大小时保持正确宽高比的关键image是用于计算比率的算法
*,即。
*
* NewHeight = GivenWidth *(OriginalHeight / OriginalWidth)
*或
*
* NewWidth = GivenHeight *(OriginalWidth / OriginalHeight)
*
*此计算假定Given ...是图像
*应调整大小的维度。
*一旦我们知道这一点,我们可以将它乘以原始图像的方面,
*,这将为我们提供我们需要的另一方价值。因此,假设
*原始图片的宽度为1000,高度为1600,我们希望将
*的大小调整为宽度500:
*
* First找到方面:(1600/1000)=方面1.6
*现在将方面乘以所需的新宽度:1.6 * 500
*乘法的结果是800,这是我们的高度应该是
*换句话说:
* 800 = 500 *(1600/1000)
*因此得到的图像的高度为800,宽度为500.
*
* /
imageWidth = static_cast< real>(pImage-> GetWidth());
imageHeight = static_cast< real>(pImage-> GetHeight());
// 如果宽度或高度小于窗口,请使用图片大小
if (imageWidth< destRect.Width)
destRect.Width = imageWidth;
if (imageHeight< destRect.Height)
destRect.Height = imageHeight;
if (imageWidth> destRect.Width)
{
// 如果宽于窗口,则按纵横比调整高度
aspect = imageHeight / imageWidth;
REAL newHeight = destRect.Width * aspect;
// 但不要比窗口更深
if (newHeight< = destRect.Height)
destRect.Height = newHeight;
}
if (imageHeight> destRect.Height)
{
// 如果深于窗口,则按宽高比调整宽度
aspect = imageWidth / imageHeight;
destRect.Width = destRect.Height * aspect;
}
看起来我有点和蔼可亲...... :)
i使用了三条规则。
并且工作正常... yuhoo。
uc.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
img = new 位图(item.FullName);
uc.pictureBox1.Image = img;
if (img.Height > = uc.pictureBox1.Height)
{
// 10/5 = 8 / x>> x =(8 * 5)/ 10>> x = 4(10/5 = 2,8 / 4 = 2)
uc.Height =(uc.pictureBox1.Width * img.Height)/ img.Width;
}
// 其中uc =包含公共pictureBox1的userControl。
Bitmap img;//outside current method.
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
img = new Bitmap(file.FullName);
if (img.Height >= pictureBox1.Height )
{
//how do i change the img dimension?
//i want to Set the height or width of the loaded image, to fit it properly into my picturebox.
//if the image mode is vertical, my picturebox must have height> width. The height of the img will transform how high the picturebox high will be.
}
pictureBox1.Image = img;
}
解决方案
I found this on a blog some years ago, but cannot find the original. This is my modified version of the blog. It's C++ but the principles are the same, converting it to C# should not be too difficult:
// get the destination co-ordinates ::GetClientRect(hWnd, &rectWindow); destRect.X = static_cast<real>(rectWindow.left); destRect.Y = static_cast<real>(rectWindow.top); destRect.Width = static_cast<real>((rectWindow.right - rectWindow.left)); destRect.Height = static_cast<real>((rectWindow.bottom - rectWindow.top)); /* * The key to keeping the correct aspect ratio while resizing an image is the algorithm * used to calculate the ratio, viz. * * NewHeight = GivenWidth * (OriginalHeight / OriginalWidth) * or * * NewWidth = GivenHeight * (OriginalWidth / OriginalHeight) * * This calculation assumes that the "Given..." is the dimension the image * should be resized to. * Once we know this, we can multiply it by the original image’s aspect, * and that will give us the other side's value we need. So, assuming the * original image has a width of 1000 and a height of 1600 and we want it * to be resized to a width of 500: * * First find the aspect: (1600 / 1000) = aspect of 1.6 * Now multiply the aspect by the desired new width: 1.6 * 500 * The result of that multiplication is 800, which is what our height should be * In other words: * 800 = 500 * (1600 / 1000) * So the resulting image would have a height of 800 and a width of 500. * */ imageWidth = static_cast<real>(pImage->GetWidth()); imageHeight = static_cast<real>(pImage->GetHeight()); // if width or height is less than window, use image size if (imageWidth < destRect.Width) destRect.Width = imageWidth; if (imageHeight < destRect.Height) destRect.Height = imageHeight; if (imageWidth > destRect.Width) { // if wider than window then adjust height by aspect ratio aspect = imageHeight / imageWidth; REAL newHeight = destRect.Width * aspect; // but don't make it deeper than the window if (newHeight <= destRect.Height) destRect.Height = newHeight; } if (imageHeight > destRect.Height) { // if deeper than window then adjust width by aspect ratio aspect = imageWidth / imageHeight; destRect.Width = destRect.Height * aspect; }
it seem i got a stroke of geniality...:)
i used the three rule.
And is working perfectly... yuhoo.
uc.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; img = new Bitmap(item.FullName); uc.pictureBox1.Image = img; if (img.Height >= uc.pictureBox1.Height) { //10/5 = 8/x >> x= (8*5)/10 >> x=4 (10/5=2, 8/4=2) uc.Height = (uc.pictureBox1.Width * img.Height) / img.Width; } // where uc = userControl that contain a public pictureBox1.
这篇关于如何更改图像位图尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文