C# 使图像适合图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16822138/
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
Fit Image into PictureBox
提问by Karlx Swanovski
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select Photo from Employee where EmpID LIKE '%' + @EmpID + '%' ", myDatabaseConnection))
{
SqlCommand.Parameters.AddWithValue("@EmpID", textBox1.Text);
var DS = new DataSet();
var adapter = new SqlDataAdapter(SqlCommand);
adapter.Fill(DS, "Images");
var imagesTable = DS.Tables["Images"];
var imagesRows = imagesTable.Rows;
var count = imagesRows.Count;
if (count <= 0) return;
var imageColumnValue =
imagesRows[count - 1]["Image"];
if (imageColumnValue == DBNull.Value)
return;
var data = (Byte[])imageColumnValue;
using (var stream = new MemoryStream(data))
{
pictureBox1.Image = Image.FromStream(stream);
}
}
}
If the image is too large for the pictureboxto fit. What is the code to make the image fit in the picturebox?
如果图像太大而picturebox无法容纳。使图像适合的代码是picturebox什么?
My pictureboxis squared, if the image is rectangular how to it crop and show it in the picturebox like this, the lower part of the picture will be removed.
我的picturebox是方形的,如果图像是矩形的,如何裁剪并像这样在图片框中显示,图片的下部将被删除。
回答by David C
First off, in order to have any image "resize" to fit a picturebox, you can set the PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
首先,为了让任何图像“调整大小”以适合图片框,您可以设置 PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
If you want to do clipping of the image beforehand (i.e. cut off sides or top and bottom), then you need to clearly define what behavior you want (start at top, fill the height of the pciturebox and crop the rest, or start at the bottom, fill the height of the picturebox to the top, etc), and it should be fairly simple to use the Height / Width properties of both the picturebox and the image to clip the image and get the effect you are looking for.
如果您想事先对图像进行剪裁(即切掉侧面或顶部和底部),那么您需要明确定义您想要的行为(从顶部开始,填充 pciturebox 的高度并裁剪其余部分,或从底部,将图片框的高度填充到顶部等),使用图片框和图像的高度/宽度属性来剪辑图像并获得您正在寻找的效果应该相当简单。
回答by Max
You could try changing the: SizeMode property of the PictureBox.
您可以尝试更改图片框的:SizeMode 属性。
You could also set your image as the BackGroundImage of the PictureBox and try changing the BackGroundImageLayout to the correct mode.
您还可以将图像设置为 PictureBox 的 BackGroundImage 并尝试将 BackGroundImageLayout 更改为正确的模式。
回答by matzone
I have routine in VB ..
我在 VB 中有例程..
but you should have 2 pictureboxes .. 1 for frame .. 1 for the image .. and it make keep the picture's size ratio
但是你应该有 2 个图片框 .. 1 个用于框架 .. 1 个用于图像 .. 它可以保持图片的大小比例
Assumed picFrame is the image frame and picImg is the image
假设 picFrame 是图像帧, picImg 是图像
Sub InsertPicture(ByVal oImg As Image)
Dim oFoto As Image
Dim x, y As Integer
oFoto = oImg
picImg.Visible = False
picImg.Width = picFrame.Width - 2
picImg.Height = picFrame.Height - 2
picImg.Location = New Point(1, 1)
SetPicture(picPreview, oFoto)
x = (picImg.Width - picFrame.Width) / 2
y = (picImg.Height - picFrame.Height) / 2
picImg.Location = New Point(x, y)
picImg.Visible = True
End Sub
I'm sure you can make it as C# ....
我相信你可以把它变成 C# ....
回答by Gauthier G. Letellier
You could use the SizeMode property of the PictureBox Control and set it to Center. This will match the center of your image to the center of your picture box.
您可以使用图片框控件的 SizeMode 属性并将其设置为中心。这将使图像的中心与图片框的中心相匹配。
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
Hope it could help.
希望它能有所帮助。
回答by Chandima
Use the following lines of codes and you will find the solution...
使用以下代码行,您将找到解决方案...
pictureBox1.ImageLocation = @"C:\Users\Desktop\mypicture.jpg";
pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;
回答by Nicolas Gong
You can set picturebox's SizeModeproperty to PictureSizeMode.Zoom, this will increase the size of smaller images or decrease the size of larger images to fill the PictureBox
您可以将图片框的SizeMode属性设置为PictureSizeMode.Zoom,这将增加较小图像的大小或减小较大图像的大小以填充图片框
回答by RBK
Have a look at the sizemode property of the picturebox.
查看图片框的 sizemode 属性。
pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;
回答by DAG
The PictureBox.SizeMode options are missing a "fill" or "cover" mode which would be like zoom except with cropping to ensure you're filling the picture box. In CSS it's the "cover" option.
PictureBox.SizeMode 选项缺少“填充”或“覆盖”模式,除了裁剪以确保填充图片框外,它类似于缩放。在 CSS 中,它是“封面”选项。
This code should enable that:
此代码应启用:
static public void fillPictureBox(PictureBox pbox, Bitmap bmp)
{
pbox.SizeMode = PictureBoxSizeMode.Normal;
bool source_is_wider = (float)bmp.Width / bmp.Height > (float)pbox.Width / pbox.Height;
var resized = new Bitmap(pbox.Width, pbox.Height);
var g = Graphics.FromImage(resized);
var dest_rect = new Rectangle(0, 0, pbox.Width, pbox.Height);
Rectangle src_rect;
if (source_is_wider)
{
float size_ratio = (float)pbox.Height / bmp.Height;
int sample_width = (int)(pbox.Width / size_ratio);
src_rect = new Rectangle((bmp.Width - sample_width) / 2, 0, sample_width, bmp.Height);
}
else
{
float size_ratio = (float)pbox.Width / bmp.Width;
int sample_height = (int)(pbox.Height / size_ratio);
src_rect = new Rectangle(0, (bmp.Height - sample_height) / 2, bmp.Width, sample_height);
}
g.DrawImage(bmp, dest_rect, src_rect, GraphicsUnit.Pixel);
g.Dispose();
pbox.Image = resized;
}


