C#中Winform开发实现点击按钮上传与保存图片代码
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog HdhCmsOpenFile = new OpenFileDialog();
HdhCmsOpenFile.Title = "选择要上传的图片";
HdhCmsOpenFile.Filter = "图片文件(*.png,*.jpg,*.gif)|*.png;*.jpg;*.gif";
if (HdhCmsOpenFile.ShowDialog().Equals(DialogResult.OK))
{
string hdhcmsFileName = HdhCmsOpenFile.FileName;
using (FileStream hdhcmsFs = new FileStream(hdhcmsFileName, FileMode.Open, FileAccess.Read))
{
byte[] hdhcmsByte = new byte[hdhcmsFs.Length];
hdhcmsFs.Read(hdhcmsByte, 0, hdhcmsByte.Length);
Image hdhcmsImg = Image.FromStream(hdhcmsFs);
Bitmap hdhcmsPic = new Bitmap(hdhcmsImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = hdhcmsPic;
string hdhcmsPath = (Application.StartupPath + "\\Upload\\").ToLower().Replace("\\bin\\debug\\", "\\");
if (!Directory.Exists(hdhcmsPath)) { Directory.CreateDirectory(hdhcmsPath); }
string hdhcmsFileExt = Path.GetExtension(hdhcmsFileName);
string hdhcmsNewFile = DateTime.Now.ToString("yyyyMMddhhmmss") + hdhcmsFileExt;
string hdhcmsAllName = hdhcmsPath + hdhcmsNewFile;
hdhcmsImg.Save(hdhcmsAllName);
}
}
}
查看更多关于C#中Winform开发实现点击按钮上传与保存图片代码的详细内容...