注意:转换为ICO后效果不好.

源代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace paomiangege
{
    public class ImageConvert
    {
        private int ICON_W = 64;
        private int ICON_H = 64;

public ImageConvert()
        {
        }

//fileinpath,origaly picture file path,fileoutpath save filepath,index the ImageFormat you want to convert to
        public string Convert(string fileinpath, string fileoutpath, string index)
        {
            try
            {
                Bitmap bitmap = new Bitmap(fileinpath);
                index = index.ToLower();
                switch (index)
                {
                    case "jpg":  bitmap.Save(fileoutpath, ImageFormat.Jpeg); break;
                    case "jpeg": bitmap.Save(fileoutpath, ImageFormat.Jpeg); break;
                    case "bmp":  bitmap.Save(fileoutpath, ImageFormat.Bmp); break;
                    case "png": bitmap.Save(fileoutpath, ImageFormat.Png); break;
                    case "emf": bitmap.Save(fileoutpath, ImageFormat.Emf); break;
                    case "gif": bitmap.Save(fileoutpath, ImageFormat.Gif); break;
                    case "wmf": bitmap.Save(fileoutpath, ImageFormat.Wmf); break;
                    case "exif": bitmap.Save(fileoutpath, ImageFormat.Exif); break;
                    case "tiff":
                        {
                            Stream stream = File.Create(fileoutpath);
                            bitmap.Save(stream, ImageFormat.Tiff);
                            stream.Close();
                        } break;
                    case "ico":
                        {
                            Icon ico;
                            Stream stream = File.Create(fileoutpath);
                            ico = BitmapToIcon(bitmap, false);
                            ico.Save(stream);       //   save the icon
                            stream.Close();
                        }; break;
                    default: return "Error!";
                }
                return "Success!";
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }

public string Convert(string fileinpath, string fileoutpath, string index,int width,int height)
        {
            if (width <= 0 || height <= 0)
                return "error!size illegal!";
            try
            {
                Bitmap mybitmap = new Bitmap(fileinpath);
                Bitmap bitmap = new Bitmap(mybitmap, width, height);
                index = index.ToLower();
                switch (index)
                {
                    case "jpg": bitmap.Save(fileoutpath, ImageFormat.Jpeg); break;
                    case "jpeg": bitmap.Save(fileoutpath, ImageFormat.Jpeg); break;
                    case "bmp": bitmap.Save(fileoutpath, ImageFormat.Bmp); break;
                    case "png": bitmap.Save(fileoutpath, ImageFormat.Png); break;
                    case "emf": bitmap.Save(fileoutpath, ImageFormat.Emf); break;
                    case "gif": bitmap.Save(fileoutpath, ImageFormat.Gif); break;
                    case "wmf": bitmap.Save(fileoutpath, ImageFormat.Wmf); break;
                    case "exif": bitmap.Save(fileoutpath, ImageFormat.Exif); break;
                    case "tiff":
                        {
                            Stream stream = File.Create(fileoutpath);
                            bitmap.Save(stream, ImageFormat.Tiff);
                            stream.Close();
                        } break;
                    case "ico":
                        {
                            if (height > 256 || width > 256)//ico maxsize 256*256
                                return "Error!Size illegal!";
                            Icon ico;
                            ICON_H = height;
                            ICON_W = width;
                            Stream stream = File.Create(fileoutpath);
                            ico = BitmapToIcon(mybitmap, true);
                            ico.Save(stream);       //   save the icon
                            stream.Close();
                        }; break;
                    default: return "Error!";
                }
                return "Success!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

private Icon BitmapToIcon(Bitmap obm, bool preserve)
        {
            Bitmap bm;
            // if not preserving aspect
            if (!preserve)        // if not preserving aspect
                bm = new Bitmap(obm, ICON_W, ICON_H);  //   rescale from original bitmap

// if preserving aspect drop excess significance in least significant direction
            else          // if preserving aspect
            {
                Rectangle rc = new Rectangle(0, 0, ICON_W, ICON_H);
                if (obm.Width >= obm.Height)   //   if width least significant
                {          //     rescale with width based on max icon height
                    bm = new Bitmap(obm, (ICON_H * obm.Width) / obm.Height, ICON_H);
                    rc.X = (bm.Width - ICON_W) / 2;  //     chop off excess width significance
                    if (rc.X < 0) rc.X = 0;
                }
                else         //   if height least significant
                {          //     rescale with height based on max icon width
                    bm = new Bitmap(obm, ICON_W, (ICON_W * obm.Height) / obm.Width);
                    rc.Y = (bm.Height - ICON_H) / 2; //     chop off excess height significance
                    if (rc.Y < 0) rc.Y = 0;
                }
                bm = bm.Clone(rc, bm.PixelFormat);  //   bitmap for icon rectangle
            }

// create icon from bitmap
            Icon icon = Icon.FromHandle(bm.GetHicon()); // create icon from bitmap
            bm.Dispose();        // dispose of bitmap
            return icon;        // return icon
        }
    }
}

利用C#转换图片格式及转换为ico的更多相关文章

  1. 使用IMAGEMAGICK的CONVERT工具批量转换图片格式

    使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...

  2. java批量转换图片格式

    废话不多直接上代码,代码其实也不多.... package com.qiao.testImage; import java.awt.image.BufferedImage; import java.i ...

  3. Mac电脑如何转换图片格式?ImageWell for Mac转换图片格式教程

    想用Mac电脑转换图片格式?我想你可以借助ImageWell for Mac软件!ImageWell是一款简单好用的的图像处理工具,具有显示,编辑,处理,保存等功能.下面小编来为大家演示在Mac电脑上 ...

  4. 【最简单】不用ps也可以批量转换图片格式

    不废话直接开始~ 1.新建文件夹,把需要转换的图片放进去,如图: 2.文件夹里建一txt文本,重点来了!txt文本的内容,如果是jpg转为png,则输入“ren *.jpg *.png”,同理png转 ...

  5. ubuntu 转换图片格式的方法(sam2p, imagemagick)

    (1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...

  6. 利用jks2pfx转换keystore格式的证书为pfs格式(含秘钥和证书的形式)

    利用java语言写的openssl转换证书格式工具,使用方法如下所示: Java KeyStore文件转换为微软的.pfx文件和OpenSSL的PEM格式文件(.key + .crt)运行方式:JKS ...

  7. FreeImage库如何转换图片格式?

    FreeImage下载地址:http://freeimage.sourceforge.net/ //freeimagemain.h #ifndef FREEIMAGEMAIN_H #define FR ...

  8. 利用kindlegen实现txt格式小说转换为mobi格式小说(C++实现)

    一直以来喜欢在kindle上看小说,kindle不伤眼,也可以帮助控制玩手机的时间.但在kindle上看txt格式的网络小说就很头疼了,这类小说在kindle上是没有目录的,而且篇幅巨长.所以一直以来 ...

  9. python转换图片格式

    在图片所在的路径下,打开命令窗口 bmeps -c picturename.png picturename.eps

随机推荐

  1. Elixir游戏服设计四

    上章说到我们要引入syn https://github.com/ostinelli/syn/ 看过文档,它并没有直接提供{via, Module, Name} 相关的方法.我们需要封装一下. Name ...

  2. 委托、事件、Observer观察者模式的使用解析二

    一.设计模式-Observer观察者模式 Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新.Observer模式是一种 ...

  3. MySQL的备份与还原以及常用数据库查看命令

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serv ...

  4. 正则表达式与grep和sed

    正则表达式与grep和sed 目录 1.正则表达式 2.grep 3.sed grep和sed需要正则表达式,我们需要注意的正则表达式与通配符用法的区分. 1.正则表达式 REGEXP,正则表达式:由 ...

  5. Spring MVC 页面跳转时传递参数

    页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...

  6. Node.js之异步流控制

    前言 在没有深度使用函数回调的经验的时候,去看这些内容还是有一点吃力的.由于Node.js独特的异步特性,才出现了"回调地狱"的问题,这篇文章中,我比较详细的记录了如何解决异步流问 ...

  7. Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程

    事前准备 最近给游戏添加了苹果的内购,这一块的东西也是刚刚做完,总结一下,其实这里不管是游戏还是我们普通的App添加内购这一块的东西都是差不多的,多出来的部分就是我们Lua和OC的交互的部分,以前刚开 ...

  8. JavaScript面向对象深入理解原型

    原型模式 function Person(){ } Person.prototype.name="Ewarm"; Person.prototype.age="29&quo ...

  9. C++函数重载和函数模板(04)

    函数重载 函数重载可以使一个函数名具有多种功能,即具有“多种形态”,这种特性称为多态性. C++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...

  10. Go Global 之怎样在全球Azure上使用Azure Free Account

    随着中国用户出海的越来越多,同学们自学Azure Global 功能的积极性也越来越高.怎样开启Azure Global 账号,有哪些Global Azure的功能可以免费使用,能不能用国内的信用卡和 ...