利用C#转换图片格式及转换为ico
注意:转换为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的更多相关文章
- 使用IMAGEMAGICK的CONVERT工具批量转换图片格式
使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...
- java批量转换图片格式
废话不多直接上代码,代码其实也不多.... package com.qiao.testImage; import java.awt.image.BufferedImage; import java.i ...
- Mac电脑如何转换图片格式?ImageWell for Mac转换图片格式教程
想用Mac电脑转换图片格式?我想你可以借助ImageWell for Mac软件!ImageWell是一款简单好用的的图像处理工具,具有显示,编辑,处理,保存等功能.下面小编来为大家演示在Mac电脑上 ...
- 【最简单】不用ps也可以批量转换图片格式
不废话直接开始~ 1.新建文件夹,把需要转换的图片放进去,如图: 2.文件夹里建一txt文本,重点来了!txt文本的内容,如果是jpg转为png,则输入“ren *.jpg *.png”,同理png转 ...
- ubuntu 转换图片格式的方法(sam2p, imagemagick)
(1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...
- 利用jks2pfx转换keystore格式的证书为pfs格式(含秘钥和证书的形式)
利用java语言写的openssl转换证书格式工具,使用方法如下所示: Java KeyStore文件转换为微软的.pfx文件和OpenSSL的PEM格式文件(.key + .crt)运行方式:JKS ...
- FreeImage库如何转换图片格式?
FreeImage下载地址:http://freeimage.sourceforge.net/ //freeimagemain.h #ifndef FREEIMAGEMAIN_H #define FR ...
- 利用kindlegen实现txt格式小说转换为mobi格式小说(C++实现)
一直以来喜欢在kindle上看小说,kindle不伤眼,也可以帮助控制玩手机的时间.但在kindle上看txt格式的网络小说就很头疼了,这类小说在kindle上是没有目录的,而且篇幅巨长.所以一直以来 ...
- python转换图片格式
在图片所在的路径下,打开命令窗口 bmeps -c picturename.png picturename.eps
随机推荐
- 干货,比较全面的c#.net公共帮助类
比较全面的c#帮助类 比较全面的c#帮助类,日常工作收集,包括前面几家公司用到的,各式各样的几乎都能找到,所有功能性代码都是独立的类,类与类之间没有联系,可以单独引用至项目,分享出来,方便大家,几乎都 ...
- Java视频扩展知识 线程池的了解
Java视频扩展知识 线程池的了解 1.简单介绍: Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的 ...
- 架构师必备软件:安装Dubbo注册中心(Zookeeper-3.4.6)
Dubbo建议使用Zookeeper作为服务的注册中心. http://www.roncoo.com/details?cid=f614343765bc4aac8597c6d8b38f06fd 注册中心 ...
- 即时通信系统Openfire分析之七:集群配置
前言 写这章之前,我犹豫了一会.在这个时候提集群,从章节安排上来讲,是否合适?但想到上一章<路由表>的相关内容,应该不至于太突兀.既然这样,那就撸起袖子干吧. Openfire的单机并发量 ...
- bzoj1812 [Ioi2005]riv
riv 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄--名叫B ...
- win10 uwp 读写XML
UWP 对 读写 XML做了一些修改,但和之前 WPF 的方法没有大的区别. 我们先来说下什么是 XML , XML 其实是 树结构,可以表达复杂的结构,所以在定制要求高的.或其他方面如json 做不 ...
- 使用Hexo+Github一步步搭建属于自己的博客(基础)
前言:电脑系统为window 10专业版,64位 相关步骤: 1.安装Node.js和配置好Node.js环境,打开cmd命令行,成功界面如下 2.安装Git和配置好Git环境,安装成功的象征就是在电 ...
- Web Fragment在项目中的使用
Web Fragment 是什么 - 它是在 servlet 3.0开始支持的,可以把一个dy web项目拆分为多个项目,解耦合,使其在项目中开发效率提高,下面我演示简单的项目创建过程 用eclips ...
- 【NOIP2016提高组】 Day2 T2 蚯蚓
题目传送门:https://www.luogu.org/problemnew/show/P2827 自测时被题面所误导...,题面中说逢t的倍数才输出答案,以为有什么玄妙的方法直接将m次操作变成了m/ ...
- JS难点--面向对象(封装)
我觉得js的难点之一就是面向对象编程. 面向对象 它是一种编程思想,它在写法上比面向过程相对来说复杂一些: 以下是我学习中关于面向对象的知识点总结: 1.什么是对象 从广义上说,"一切皆 ...