当前位置: 移动技术网 > IT编程>开发语言>c# > C#简易图片格式转换器实现方法

C#简易图片格式转换器实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#简易图片格式转换器实现方法。分享给大家供大家参考,具体如下: 在窗体上放一个picturebox,menustrip.在菜单上键入两个按钮,分别为“文件

本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网