/// <summary>
/// 将图片上传到PictureBox控件中
/// </summary>
/// <param name="picBox">PictureBox</param>
/// <param name="picInfo">返回的图片信息</param>
public void SetPictureToCtrl(PictureBox picBox, out string picInfo)
{
OpenFileDialog ofdPic = new OpenFileDialog();
ofdPic.Filter = "JPG(*.JPG;*.JPEG);|*.jpg;*.jpeg;";
ofdPic.FilterIndex = 1;
ofdPic.RestoreDirectory = true;
ofdPic.FileName = "";
if (ofdPic.ShowDialog() == DialogResult.OK)
{
string sPicPaht = ofdPic.FileName.ToString();
FileInfo fiPicInfo = new FileInfo(sPicPaht);
long lPicLong = fiPicInfo.Length / 1024;
string sPicName = fiPicInfo.Name;
string sPicDirectory = fiPicInfo.Directory.ToString();
string sPicDirectoryPath = fiPicInfo.DirectoryName;
Bitmap bmPic = new Bitmap(sPicPaht);
if (BaseOption.Initialize.UserPhotoMaxSize > 0 && lPicLong / 1024 > BaseOption.Initialize.UserPhotoMaxSize)
{
MessageBox.Show("此文件大小为" + lPicLong / 1024 + "M;已超过最大限制的" + BaseOption.Initialize.UserPhotoMaxSize + "M范围!");
}
else
{
Point ptLoction = new Point(bmPic.Size);
if (ptLoction.X > picBox.Size.Width || ptLoction.Y > picBox.Size.Height)
{
picBox.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
picBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
picBox.LoadAsync(sPicPaht);
picInfo = "图片名称:" + sPicName + "\r\n图片大小:" + lPicLong.ToString() + " KB" + "\r\n图片像素:" + bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString();
}
picInfo = "";
}
将图片上传到PictureBox控件中
最新推荐文章于 2023-04-28 14:08:46 发布