none
winform新手 想問功課問題 RRS feed

  • 問題

  • 小弟第一次學winform

    碰到一個問題

    想請教這部分我這個button要做一個存檔的功能

    在我按下processing的button把兩張圖做合併後

    按Save Image後要存檔

    但我這邊存檔都會跳錯誤訊息並且存好的檔沒辦法開

    想請教前輩們 這個問題出在哪

    button3是Save Image的button

    附上我的程式碼

    public partial class Form1 : Form
    	{
    		Bitmap pic;
    		Bitmap pic2;
    		
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void button1_Click(object sender, EventArgs e)
    		{
    			OpenFileDialog openFile = new OpenFileDialog();
    			openFile.Filter = "All Files|*.*";
    			openFile.ShowDialog();
    			pic = new Bitmap(openFile.FileName);
    			this.pictureBox1.Image = pic;
    			
    		}
    
    		private void button4_Click(object sender, EventArgs e)
    		{
    			OpenFileDialog openFile = new OpenFileDialog();
    			openFile.Filter = "All Files|*.*";
    			openFile.ShowDialog();
    			pic2 = new Bitmap(openFile.FileName);
    			this.pictureBox2.Image = pic2;
    		}
    
    		private void button3_Click(object sender, EventArgs e)
    		{
    			SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    			saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
    			saveFileDialog1.Title = "Save an Image File";
    			saveFileDialog1.ShowDialog();
    			
    			if (saveFileDialog1.FileName != "")
    			{
    				System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
    				
    				switch (saveFileDialog1.FilterIndex)
    				{
    					case 1:
    						this.button2.Image.Save(fs,
    						  System.Drawing.Imaging.ImageFormat.Jpeg);
    						break;
    
    					case 2:
    						this.button2.Image.Save(fs,
    						  System.Drawing.Imaging.ImageFormat.Bmp);
    						break;
    
    					case 3:
    						this.button2.Image.Save(fs,
    						  System.Drawing.Imaging.ImageFormat.Gif);
    						break;
    				}
    
    				fs.Close();
    			}
    			
    		}
    
    
    		
    
    		private Image MergeImage(Image img1, Image img2)
    		{
    			Image MergedImage = default(Image);
    			int wide = 0;
    			int high = 0;
    			wide = img1.Width + img2.Width;
    			if (img1.Height >= img2.Height)
    			{
    				high = img1.Height;
    			}
    			else
    			{
    				high = img2.Height;
    			}
    			Bitmap mybmp = new Bitmap(wide, high);
    			Graphics gr = Graphics.FromImage(mybmp);
    			gr.DrawImage(img1, 0, 0);
    			gr.DrawImage(img2, img1.Width, 0);
    			MergedImage = mybmp;
    			gr.Dispose();
    			return MergedImage;
    		}
    
    		private void button2_Click(object sender, EventArgs e)
    		{
    			//MergeImage(pic, pic2);
    			MergeImage(this.pictureBox1.Image, this.pictureBox2.Image);
    		}
    	}

    2020年5月29日 上午 10:50

所有回覆