Use C # to convert the image format and ico

Source: Internet
Author: User

Note: converted to ICO does not work well.

Source code:

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
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 "jp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.