zoukankan      html  css  js  c++  java
  • 利用C#转换图片格式,还可转换为ico

    利用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
            }
        }
    }

  • 相关阅读:
    node
    github
    [模块] pdf转图片-pdf2image
    python 15 自定义模块 随机数 时间模块
    python 14 装饰器
    python 13 内置函数II 匿名函数 闭包
    python 12 生成器 列表推导式 内置函数I
    python 11 函数名 迭代器
    python 10 形参角度 名称空间 加载顺序
    python 09 函数参数初识
  • 原文地址:https://www.cnblogs.com/JoshuaDreaming/p/1863263.html
Copyright © 2011-2022 走看看