C#簡略單純圖片格局轉換器完成辦法。本站提示廣大學習愛好者:(C#簡略單純圖片格局轉換器完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#簡略單純圖片格局轉換器完成辦法正文
本文實例講述了C#簡略單純圖片格局轉換器完成辦法。分享給年夜家供年夜家參考,詳細以下:
在窗體上放一個picturebox,menustrip.在菜單上鍵入兩個按鈕,分離為“文件”,“格局”。在“文件”下創立一個子菜單“翻開”,name為menuOpen,在“格局”下創立一個子菜單“轉換格局”,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 = "";//文件名
//文件菜單下的“翻開”按鈕
private void menuOpen_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Title = "翻開文件";
of.Filter = "圖象文件|*.bmp;*.gif;*.jpg;*.png";
if (of.ShowDialog() == DialogResult.OK)
{
filename = of.FileName;
pictureBox1.Image = Image.FromFile(filename);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
//“轉換格局”按鈕
private void menuConvert_Click(object sender, EventArgs e)
{
ImageFormat[] format = { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg, ImageFormat.Png };
//ImageFormat是using System.Drawing.Imaging;下的辦法,用來指定文件的格局
Image image = Image.FromFile(filename);
SaveFileDialog sf = new SaveFileDialog();
sf.InitialDirectory = Path.GetDirectoryName(filename);//system.io下的path裡的GetDirectoryName()辦法可以前往指定途徑字符串的目次信息
sf.FileName = Path.GetFileNameWithoutExtension(filename);//前往不具有擴大名的指定途徑字符串的文件名
sf.Filter = "位圖(*.bmp)|*.bmp|交流圖象格局(*.gif)|*.gif|結合圖象專家組(*.jpg)|*.jpg;*.jpeg|可移植收集圖形(*.png)|*.png";
if (sf.ShowDialog() == DialogResult.OK)
{
image.Save(sf.FileName, format[sf.FilterIndex - 1]);//選擇下拉表的第一個,則對應數組format[0]
MessageBox.Show("格局轉換勝利", "新聞");
}
else
{
MessageBox.Show("格局轉換不勝利", "新聞");
}
}
}
}
後果圖以下:
翻開一幅jpg圖,轉換為bitmap


願望本文所述對年夜家C#法式設計有所贊助。