C# 自动调整图片框中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13394850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autosize Image in Picture Box
提问by Jay
I'm doing an imageviewer. I've already done importing the image in the picture box.
我正在做一个图像查看器。我已经完成了在图片框中导入图像。
Which code should be used to autosize the image in the picture box? Here's my code in Viewing the image in picture box.
应该使用哪个代码来自动调整图片框中的图像?这是我在查看图片框中的图像中的代码。
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pFileNames = openFileDialog.FileNames;
pCurrentImage = 0;
ImageView();
}
}
protected void ImageView()
{
if (pCurrentImage >= 0 && pCurrentImage <= pFileNames.Length - 1)
{
pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
}
}
采纳答案by SeToY
Take a look at the SizeModeproperty of the PictureBox: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx
看看SizeMode属性PictureBox:http: //msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx
Set this to AutoSizeand you're ready to go.
将此设置为AutoSize,您就可以开始了。
回答by Justin Harvey
You could try setting the SizeMode property on the PictureBox to AutoSize.
您可以尝试将 PictureBox 上的 SizeMode 属性设置为 AutoSize。
回答by Niranjan Singh
Check PictureBox.SizeMode Propertyand set it by PictureBoxSizeMode Enumerationas you want PictureBox control to do while displaying the image.
检查PictureBox.SizeMode 属性并通过PictureBoxSizeMode Enumeration设置它,因为您希望 PictureBox 控件在显示图像时执行该操作。
- AutoSize means that the PictureBox is going to fit to the image.
- AutoSize 意味着 PictureBox 将适合图像。
If you want image fit to the pictureBox then set the sizemode to StretchImage
如果您希望图像适合图片框,则将 sizemode设置为StretchImage
// Set the SizeMode property to the StretchImage value.
// This will enlarge the image as needed to fit into
// the PictureBox.
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

