• C# 图片格式转换


    在日常工作中,经常需要不同格式(JPG,PNG,BMP,GIF,ICO)的图片,有时还需要进行图片格式的相互转换,本文以一个简单的小例子,简述图片格式转换的常见方法,仅供学习分享使用,如有不足之处,还请指正。

    涉及知识点

    • OpenFileDialog 打开文件对话框,用于选择文件,可以设置过滤后缀。
    • FolderBrowserDialog 文件夹选择对话框,用于选择一个文件夹,可以新增。
    • ImageFormat 图片类型枚举。
    • Bitmap 位图对象,包含对应的属性和内容。
    • Stream 流对象的基类。
    • FlowLayoutPanel 流式布局容器,所添加的元素,以横向或纵向依次排列。

    示例效果图

    图片转换器的示例效果图如下:

    核心代码

     打开图片

     1         /// <summary>
     2         /// 打开图片
     3         /// </summary>
     4         /// <param name="sender"></param>
     5         /// <param name="e"></param>
     6         private void btnOpen_Click(object sender, EventArgs e)
     7         {
     8 
     9             this.fileDialog.Filter = fileFilter;
    10             this.fileDialog.Multiselect = true;
    11             this.fileDialog.CheckFileExists = true;
    12             if (fileDialog.ShowDialog() == DialogResult.OK)
    13             {
    14                 string[] fileNames = this.fileDialog.FileNames;
    15                 foreach(string fileName in fileNames)
    16                 {
    17                     Bitmap bmp = new Bitmap(fileName);
    18                     //保存图片名称
    19                     bmp.Tag = Path.GetFileNameWithoutExtension(fileName);
    20                     PictureBox box = new PictureBox();
    21                     box.Image = bmp;
    22                     box.Width = 105;
    23                     box.Height = 150;
    24                     box.BorderStyle = BorderStyle.FixedSingle;
    25                     box.Padding = new Padding(2);
    26                     this.flowPnl.Controls.Add(box);
    27                 }
    28                 this.txtFile.Text = Path.GetDirectoryName(fileNames[0]);
    29 
    30             }
    31         }
    View Code

    转换图片格式

     1         /// <summary>
     2         /// 转换图片
     3         /// </summary>
     4         private void convertImage(string dir, string filter,Bitmap bmp)
     5         {
     6             string filePath = Path.Combine(dir, string.Format("{0}.{1}", bmp.Tag.ToString(), filter.ToLower()));
     7             switch (filter)
     8             {
     9                 case "JPG":
    10                     bmp.Save(filePath, ImageFormat.Jpeg);
    11                     break;
    12                 case "PNG":
    13                     bmp.Save(filePath, ImageFormat.Png);
    14                     break;
    15                 case "GIF":
    16                     bmp.Save(filePath, ImageFormat.Gif);
    17                     break;
    18                 case "BMP":
    19                     bmp.Save(filePath, ImageFormat.Bmp);
    20                     break;
    21                 case "ICO":
    22                     Stream stream = File.Create(filePath);
    23                     Icon icon = Icon.FromHandle(bmp.GetHicon());
    24                     icon.Save(stream);       //   save the icon
    25                     stream.Close();
    26                     break;
    27             }
    28         }
    View Code

    如果需要示例的源码,可以点击链接进行下载

    源码链接

    备注

    题李凝幽居

    作者:贾岛  唐代

    闲居少邻并,草径入荒园。
    鸟宿池边树,僧敲月下门。
    过桥分野色,移石动云根。
    暂去还来此,幽期不负言。

  • 相关阅读:
    hdu1150&&POJ1325 Machine Schedule---最小点覆盖
    hdu-1068&&POJ1466 Girls and Boys---最大独立集
    hdu-2680 Choose the best route---dijkstra+反向存图或者建立超级源点
    hdu-1317 XYZZY---Floyd判连通+bellman最短路
    hdu-1874 畅通工程续---模板题
    hdu-2112 HDU Today---dijkstra+标号
    hdu-2066 一个人的旅行---模板题
    hdu-3790 最短路径问题---dijkstra两重权值
    hdu-2544 最短路---模板题
    BZOJ3529: [Sdoi2014]数表
  • 原文地址:https://www.cnblogs.com/hsiang/p/13416132.html
Copyright © 2020-2023  润新知