c#

位置:IT落伍者 >> c# >> 浏览文章

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


发布日期:2019年08月25日
 
利用C#转换图片格式,还可转换为ico

注意:转换为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

}

}

}

上一篇:.net WinForm用户控件开发:用户控件下拉式属性设置

下一篇:C#体验Microsoft.NET平台基础构造