c#學習之基礎篇(動態開啟,顯示,儲存,另存新檔 圖片)[待續]

來源:互聯網
上載者:User

聲明:基礎知識來自於此:http://blog.163.com/chenjunfeng_2007/blog/static/119385083201019102742570/

一、開啟:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /*用ToolStripMenuItem實現開啟本地圖片*/

        private void 開啟ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String picPath;
            //filter寫法,"開啟對話方塊下部索要顯示內容|真正的過濾尾碼名"
            openFileDialog1.Filter = "圖片檔案(jpg,jpeg,bmp,gif,ico,pen,tif)|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
            openFileDialog1.ShowDialog();
            picPath = openFileDialog1.FileName;
            if(File.Exists(picPath))
            {
                pictureBox1.Image = Image.FromFile(picPath);
            }
        }

    /*用ToolStripMenuItem實現開啟本地圖片*/

   private void 儲存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Stream FileStream;
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "儲存檔案執行個體";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif"; 

            saveImageDialog.ShowDialog();

            if (saveImageDialog.FileName != null)
            {
                //System.IO.Stream fs = (System.IO.Stream)saveImageDialog.OpenFile();
                //FileStream filestream = new FileStream(openFileDialog1.FileName.ToString(),FileMode.Open,FileAccess.Read);
                String fileName = saveImageDialog.FileName.ToString();

                if(fileName!="" && fileName!=null)
                {

                    /*該字串中的每個字元都被認為具有數字位置:第一個字元的位置是 1,第二個字元的位置是 2,依此類推。

                      如果未指定第三個參數,將返回從第二個參數指定的位置開始直到字串結尾的子字串。

                      如果參數不是字串類型,將先使用 string() 函數轉換為字串,然後計算該轉換的結果。
                      substring("12345",2,3) ---->(234)
                      substring("12345",2) ------>(2345)*/

                    /*此處用fileExtName作為switch語句的開關項,但有編譯期錯誤*/

                    String fileExtName = fileName.Substring(fileName.LastIndexOf(".") , 1).ToString() ;
                    System.Drawing.Imaging.ImageFormat imageformat= null;

                    if(fileExtName!=null)
                    {
                        switch(saveImageDialog.FilterIndex)
                        {
                            case 1:
                               imageformat=System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case 2:
                               imageformat=System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                        }
                        if(imageformat==null)
                        {
                            imageformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                        }

                        try
                        {
                            this.pictureBox1.Image.Save(fileName,imageformat);
                        }
                        catch
                        {
                            MessageBox.Show("sorry,儲存圖片出現異常");
                        }
                    }
                }
            }
        }

    }
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.