欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法

 更新時(shí)間:2015年11月28日 12:33:11   作者:期待秋天的葉  
這篇文章主要介紹了C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法,涉及C#基于WinForm操作圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

在窗體上放一個(gè)picturebox,menustrip.在菜單上鍵入兩個(gè)按鈕,分別為“文件”,“格式”。在“文件”下創(chuàng)建一個(gè)子菜單“打開(kāi)”,name為menuOpen,在“格式”下創(chuàng)建一個(gè)子菜單“轉(zhuǎn)換格式”,name為menuConvert. 

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.Drawing.Imaging; 
using System.IO; 
namespace WindowsFormsApplication51 
{ 
  public partial class Form1 : Form 
  { 
    public Form1() 
    { 
      InitializeComponent(); 
    } 
    string filename = "";//文件名 
    //文件菜單下的“打開(kāi)”按鈕 
    private void menuOpen_Click(object sender, EventArgs e) 
    { 
      OpenFileDialog of = new OpenFileDialog(); 
      of.Title = "打開(kāi)文件"; 
      of.Filter = "圖像文件|*.bmp;*.gif;*.jpg;*.png"; 
      if (of.ShowDialog() == DialogResult.OK) 
      { 
        filename = of.FileName; 
        pictureBox1.Image = Image.FromFile(filename); 
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
      } 
    } 
    //“轉(zhuǎn)換格式”按鈕 
    private void menuConvert_Click(object sender, EventArgs e) 
    {   
      ImageFormat[] format = { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg, ImageFormat.Png }; 
      //ImageFormat是using System.Drawing.Imaging;下的方法,用來(lái)指定文件的格式 
      Image image = Image.FromFile(filename); 
      SaveFileDialog sf = new SaveFileDialog(); 
      sf.InitialDirectory = Path.GetDirectoryName(filename);//system.io下的path里的GetDirectoryName()方法可以返回指定路徑字符串的目錄信息 
      sf.FileName = Path.GetFileNameWithoutExtension(filename);//返回不具有擴(kuò)展名的指定路徑字符串的文件名 
      sf.Filter = "位圖(*.bmp)|*.bmp|交換圖像格式(*.gif)|*.gif|聯(lián)合圖像專(zhuān)家組(*.jpg)|*.jpg;*.jpeg|可移植網(wǎng)絡(luò)圖形(*.png)|*.png"; 
      if (sf.ShowDialog() == DialogResult.OK) 
      { 
        image.Save(sf.FileName, format[sf.FilterIndex - 1]);//選擇下拉表的第一個(gè),則對(duì)應(yīng)數(shù)組format[0] 
        MessageBox.Show("格式轉(zhuǎn)換成功", "消息"); 
      } 
      else 
      { 
        MessageBox.Show("格式轉(zhuǎn)換不成功", "消息"); 
      } 
    } 
  } 
}

效果圖如下:

打開(kāi)一幅jpg圖,轉(zhuǎn)換為bitmap

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論