Kar*_*ski 48 c# image picturebox winforms
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);
DataSet DS = new DataSet();
SqlDataAdapter 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果图像太大而picturebox
无法适应.什么是使图像适合的代码picturebox
?我picturebox
的平方,如果图像是矩形如何将其裁剪并显示在像PictureBox的这个,画面的下部将被删除.
Dav*_*d C 88
首先,为了使任何图像"调整大小"以适应图片框,您可以设置 PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
如果你想事先裁剪图像(即切掉边或顶部和底部),那么你需要清楚地定义你想要的行为(从顶部开始,填充pciturebox的高度并裁剪其余部分,或者从在底部,将图片框的高度填充到顶部等,并且使用图片框和图像的高度/宽度属性来剪裁图像并获得您正在寻找的效果应该相当简单.
小智 20
使用以下代码行,您将找到解决方案......
pictureBox1.ImageLocation = @"C:\Users\Desktop\mypicture.jpg";
pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;
Run Code Online (Sandbox Code Playgroud)
RBK*_*RBK 11
看看picturebox的sizemode属性.
pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;
Run Code Online (Sandbox Code Playgroud)
PictureBox.SizeMode 选项缺少“填充”或“覆盖”模式,这类似于缩放,但需要进行裁剪以确保填充图片框。在 CSS 中,它是“覆盖”选项。
此代码应该能够:
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;
}
Run Code Online (Sandbox Code Playgroud)