熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

利用C#轉換圖片格式,還可轉換為ico

2022-06-13   來源: .NET編程 

  注意:轉換為ICO後效果不好

  源代碼:

  using System;

  using SystemCollectionsGeneric;

  using SystemText;

  using SystemDrawing;

  using SystemDrawingImaging;

  using SystemIO;

  namespace paomiangege

  {

  public class ImageConvert

  {

  private int ICON_W = ;

  private int ICON_H = ;

  public ImageConvert()

  {

  }

  //fileinpathorigaly picture file pathfileoutpath save filepathindex the ImageFormat you want to convert to

  public string Convert(string fileinpath string fileoutpath string index)

  {

  try

  {

  Bitmap bitmap = new Bitmap(fileinpath);

  index = indexToLower();

  switch (index)

  {

  case jpg:  bitmapSave(fileoutpath ImageFormatJpeg); break;

  case jpeg: bitmapSave(fileoutpath ImageFormatJpeg); break;

  case bmp:  bitmapSave(fileoutpath ImageFormatBmp); break;

  case png: bitmapSave(fileoutpath ImageFormatPng); break;

  case emf: bitmapSave(fileoutpath ImageFormatEmf); break;

  case gif: bitmapSave(fileoutpath ImageFormatGif); break;

  case wmf: bitmapSave(fileoutpath ImageFormatWmf); break;

  case exif: bitmapSave(fileoutpath ImageFormatExif); break;

  case tiff:

  {

  Stream stream = FileCreate(fileoutpath);

  bitmapSave(stream ImageFormatTiff);

  streamClose();

  } break;

  case ico:

  {

  Icon ico;

  Stream stream = FileCreate(fileoutpath);

  ico = BitmapToIcon(bitmap false);

  icoSave(stream);       //   save the icon

  streamClose();

  }; break;

  default: return Error!;

  }

  return Success!;

  }

  catch(Exception ex)

  {

  return exMessage;

  }

  }

  public string Convert(string fileinpath string fileoutpath string indexint widthint height)

  {

  if (width <= || height <= )

  return error!size illegal!;

  try

  {

  Bitmap mybitmap = new Bitmap(fileinpath);

  Bitmap bitmap = new Bitmap(mybitmap width height);

  index = indexToLower();

  switch (index)

  {

  case jpg: bitmapSave(fileoutpath ImageFormatJpeg); break;

  case jpeg: bitmapSave(fileoutpath ImageFormatJpeg); break;

  case bmp: bitmapSave(fileoutpath ImageFormatBmp); break;

  case png: bitmapSave(fileoutpath ImageFormatPng); break;

  case emf: bitmapSave(fileoutpath ImageFormatEmf); break;

  case gif: bitmapSave(fileoutpath ImageFormatGif); break;

  case wmf: bitmapSave(fileoutpath ImageFormatWmf); break;

  case exif: bitmapSave(fileoutpath ImageFormatExif); break;

  case tiff:

  {

  Stream stream = FileCreate(fileoutpath);

  bitmapSave(stream ImageFormatTiff);

  streamClose();

  } break;

  case ico:

  {

  if (height > || width > )//ico maxsize *

  return Error!Size illegal!;

  Icon ico;

  ICON_H = height;

  ICON_W = width;

  Stream stream = FileCreate(fileoutpath);

  ico = BitmapToIcon(mybitmap true);

  icoSave(stream);       //   save the icon

  streamClose();

  }; break;

  default: return Error!;

  }

  return Success!;

  }

  catch (Exception ex)

  {

  return exMessage;

  }

  }

  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( ICON_W ICON_H);

  if (obmWidth >= obmHeight)   //   if width least significant

  {          //     rescale with width based on max icon height

  bm = new Bitmap(obm (ICON_H * obmWidth) / obmHeight ICON_H);

  rcX = (bmWidth ICON_W) / ;  //     chop off excess width significance

  if (rcX < ) rcX = ;

  }

  else         //   if height least significant

  {          //     rescale with height based on max icon width

  bm = new Bitmap(obm ICON_W (ICON_W * obmHeight) / obmWidth);

  rcY = (bmHeight ICON_H) / ; //     chop off excess height significance

  if (rcY < ) rcY = ;

  }

  bm = bmClone(rc bmPixelFormat);  //   bitmap for icon rectangle

  }

  // create icon from bitmap

  Icon icon = IconFromHandle(bmGetHicon()); // create icon from bitmap

  bmDispose();        // dispose of bitmap

  return icon;        // return icon

  }

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/12504.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.