C# 手動/自動保存圖片的實例代碼
view plaincopy to clipboardprint?
//手動保存圖片
private void saveBtn_Click(object sender, System.EventArgs e)
{
bool isSave = true;
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "圖片保存";
saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
if(saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if(fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if(fileExtName!="")
{
switch(fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能存取為: jpg,bmp,gif 格式");
isSave = false;
break;
}
}
//默認保存為JPG格式
if(imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
if(isSave)
{
try
{
this.pictureBox1.Image.Save(fileName,imgformat);
//MessageBox.Show("圖片已經(jīng)成功保存!");
}
catch
{
MessageBox.Show("保存失敗,你還沒有截取過圖片或已經(jīng)清空圖片!");
}
}
}
}
}
//自動保存圖片
private void Autosave()
{
string Opath =@"D:\VedioCapture\Photo";
string photoname = DateTime.Now.Ticks.ToString();
if (Opath.Substring(Opath.Length-1, 1) != @"\")
Opath = Opath + @"\";
string path1 = Opath + DateTime.Now.ToShortDateString();
if (! Directory.Exists(path1))
Directory.CreateDirectory(path1);
//pictureBox1.Image.Save(path1 +"\\" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
//圖像的縮小
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(pictureBox1.Image);
objNewPic=new System.Drawing.Bitmap(objPic,pictureBoxShow.Width,pictureBoxShow.Height);
//objNewPic=new System.Drawing.Bitmap(objPic,320,240);//圖片保存的大小尺寸
objNewPic.Save(path1 +"\\" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
相關(guān)文章
win7中C#的winForm編程使用savefiledialog不能彈出保存窗體的解決方法
這篇文章主要介紹了win7中C#的winForm編程使用savefiledialog不能彈出保存窗體的解決方法,涉及針對線程的調(diào)用問題,是比較實用的技巧,需要的朋友可以參考下2014-12-12C#實現(xiàn)HTTP訪問類HttpHelper的示例詳解
在項目開發(fā)過程中,我們經(jīng)常會訪問第三方接口,如我們需要接入的第三方接口是Web API,這時候我們就需要使用HttpHelper調(diào)用遠程接口了。本文為大家介紹了C#實現(xiàn)HTTP訪問類HttpHelper的示例代碼,需要的可以參考一下2022-09-09一文搞懂C#實現(xiàn)讀寫文本文件中的數(shù)據(jù)
這篇文章重點給大家介紹C#實現(xiàn)讀寫文本文件中的數(shù)據(jù)的一些知識,讀取.txt文件數(shù)據(jù)的實例代碼及寫入讀取過程完整代碼,感興趣的朋友跟隨小編一起看看吧2021-06-06