注意:转换为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. 【NOIP】OpenJudge - 15:银行利息

    #include<stdio.h>//银行利息 int main() { float a,b; int i,c,d; scanf("%f%f%d",&a,&am ...

  2. Ubuntu16.04 install OpenJDK8

    1.按Ctrl + Alt + T打开终端.打开后,运行下面的命令来添加PPA:sudo add-apt-repository ppa:openjdk-r/ppa2.之后,更新系统包缓存并安装Open ...

  3. MongoDB学习教程(1)

    1.简介: MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的高性能数据 ...

  4. WPF: ShowDialog() 切换到其他应用窗口后,再切换回来无法让子窗口总在最上方

    按说ShowDialog()是模态窗口,应该在主窗口的上方,但是打开其他应用窗口再切换回来的时候,发现子窗口不见了,主窗口又不能操作. 另外子窗口设置成不在任务栏显示,只能通过Alt-Tab来切换到子 ...

  5. 在 Tomcat 8 部署多端口项目

    一般的部署途径 Tomcat 的部署途径很多,一般有如下几种: 直接将 War 包拷贝到 webapps 目录中,然后启动 Tomcat. 登陆 Tomcat 管理控制台http://localhos ...

  6. linux_base_commond_two

    1.linux privilege commond a.throught ll commond  can get follow picture d  directory    -  file   l ...

  7. 【学习】js学习笔记---字符串对象

    一.属性 1.length 字符串的长度,且不区分中英文的字节. 示例代码: var str="abcdefghijklmn"; var str1="中文汉语" ...

  8. 【Spring】浅谈spring推荐构造器注入

    一.前言 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...

  9. Hadoop(九)Hadoop IO之Compression和Codecs

    前言 前面一篇介绍了Java怎么去查看数据块的相关信息和怎么去查看文件系统.我们只要知道怎么去查看就行了!接下来我分享的是Hadoop的I/O操作. 在Hadoop中为什么要去使用压缩(Compres ...

  10. nginx+tomcat+redis sesson id主从复制

       Redis与Memcached的区别:     内存利用率:使用简单的key-value存储的话,Memcached的内存利用率更高,而如果Redis采用hash结构来做key-value存储, ...