利用C#转换图片格式,可转换为ico

原创 2008年10月03日 14:41:00
 

源代码:

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

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using S...
  • qq373591361
  • qq373591361
  • 2010年07月09日 16:11
  • 366

C#将Bitmap转换为Icon

代码如下:Bitmap bitmap=new Bitmap("test.bmp");System.IntPtr iconHandle = bitmap.GetHicon(); System.Draw...
  • xshf12345
  • xshf12345
  • 2010年10月14日 18:11
  • 2943

C#技术分享【Word转换成图片和PDF——2种方案】

木有csdn分的童鞋,可以去github clone,地址:https://github.com/stone0090/OfficeTools.Pdf2Image.Word2Image.Git. ...
  • pengxuan
  • pengxuan
  • 2016年05月15日 09:36
  • 1478

delphi 把一个ICO转换为BMP

(1)方法1 var   Icon  : TIcon;   Bitmap : TBitmap; begin    Icon  := TIcon.Create;   ...
  • yanjiaye520
  • yanjiaye520
  • 2011年08月10日 16:20
  • 1605

C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标

C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标2010-06-01 12:43 P.M.using System;using System.Drawing;using System...
  • wen_fj
  • wen_fj
  • 2010年07月16日 11:49
  • 4380

任意图片格式相互转换(ImageIO)

使用imageIO操作图片很简单,但是只能做一些比较简单的操作,下面是一个多图片格式的转换 public static void changeTypeToJPG(String srcPath,S...
  • qq962464qq
  • qq962464qq
  • 2017年03月29日 15:08
  • 426

C#图片格式转换

其实我起这个题目是有点矛盾的,因为其实这里的内容好像不太符合转换图片格式的内容,而只是在图片另存为的时候指定一下保存的格式而已,不过算了,就这样说一下吧。 在这里,首先需要到saveFileDial...
  • wangyong0921
  • wangyong0921
  • 2013年01月21日 10:37
  • 1644

用JavaScript将Canvas内容转化成图片的方法

HTML5 原文参见 http://www.webhek.com/convert-canvas-image/
  • shenzhenNBA
  • shenzhenNBA
  • 2014年05月02日 22:09
  • 4403

内存转换Image到Icon格式

有时候我们需要在内存中转换Image格式到Icon根据经验,通常我们应该可以这样做Image image = xxxx;///假设这里已经有一个Image对象System.IO.MemoryStrea...
  • zlyperson
  • zlyperson
  • 2004年03月01日 16:30
  • 872

VC++中的图像类型转换--使用开源CxImage类库

一。CxImage类库简介这只是翻译了CxImage开源项目主页上的部分简介及简单使用。CxImage类库是一个优秀的图像操作类库。它可以快捷地存取、显示、转换各种图像。有的读者可能说,有那么多优秀的...
  • roger_77
  • roger_77
  • 2006年11月04日 01:14
  • 9884
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:利用C#转换图片格式,可转换为ico
举报原因:
原因补充:

(最多只允许输入30个字)