而儲存圖檔的方式也很簡單,只要對BitMap 物件作Save 就可以
因為之前我們的範例都是使用BitMap 當作PictureBox的 Buffer
現在只要對Buffer 做SAVE就是把PICTUER 的圖形做儲存,
得知現在儲存的圖形格式有兩種方式,第一個方式是 對話方塊的過慮器
saveFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器
saveFileDialog1.InitialDirectory = Application.StartupPath; //程式啟動路徑,為預設路徑
saveFileDialog1.FileName = ""; //清除預設檔名
saveFileDialog1.ShowDialog(); //顯示對話方塊
if (openFileDialog1.FileName.Equals(""))
{
MessageBox.Show("取消選取");
}
else
{
switch (saveFileDialog1.FilterIndex)
{
case 1:
buffer.Save(saveFileDialog1.FileName , System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 2:
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg );
break;
case 3:
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif );
break;
default:
break;
}
}
第二種方式,取出字串
再由字串比對saveFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器看起來是第一種寫法比較好,第二種寫法單存練習,如何取出最後三個字元,然後將其大寫
saveFileDialog1.InitialDirectory = Application.StartupPath; //程式啟動路徑,為預設路徑
saveFileDialog1.FileName = ""; //清除預設檔名
saveFileDialog1.ShowDialog(); //顯示對話方塊
if (openFileDialog1.FileName.Equals(""))
{
MessageBox.Show("取消選取");
}
else
{
switch (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3, 3).ToUpper())
{
case "BMP":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "JPG":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "GIF":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif );
break;
case "PNG":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png );
break;
default:
MessageBox.Show ("錯誤檔名");
break;
}
}
沒有留言:
張貼留言