ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# drawing功能实现图片转换和编辑

C# drawing功能实现图片转换和编辑

原创 Linux操作系统 作者:wangwenbincom 时间:2009-08-18 12:52:43 0 删除 编辑
using System;

 using System.Collections.Generic;

 using System.Text;

 using System.IO;

 using System.Drawing;

 using System.Drawing.Imaging;

 namespace VjsdnPublishProject

 {

 public class CImageLibrary

 {

 public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }

 //检查图片大小

 public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)

 {

 byte[] bs = File.ReadAllBytes(file);

 double size = (bs.Length / 1024);

 //大于50KB

 if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;

 Image img = Image.FromFile(file);

 if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;

 return ValidateImageResult.OK;

 }

 //按宽度比例缩小图片

 public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)

 {

 Image imgOutput = imgSource;

 Size size = new Size(imgSource.Width, imgSource.Height);

 if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换

 if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)

 {

 double rate = MAX_WIDTH / (double)imgSource.Width;

 if (imgSource.Height * rate > MAX_WIDTH)

 rate = MAX_WIDTH / (double)imgSource.Height;

 size.Width = Convert.ToInt32(imgSource.Width * rate);

 size.Height = Convert.ToInt32(imgSource.Height * rate);

 imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);

 }

 return imgOutput;

 }

 //按比例缩小图片

 public static Image GetOutputSizeImage(Image imgSource, Size outSize)

 {

 Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);

 return imgOutput;

 }

 ///

 /// 由图片文件转字节

 ///

 public static byte[] GetImageBytes(string imageFileName)

 {

 Image img = Image.FromFile(imageFileName);

 return GetImageBytes(img);

 }

 ///

 /// 图片转字节

 ///

 public static byte[] GetImageBytes(Image img)

 {

 if (img == null) return null;

 try

 {

 System.IO.MemoryStream ms = new MemoryStream();

 img.Save(ms, ImageFormat.Jpeg);

 byte[] bs = ms.ToArray();

 ms.Close();

 return bs;

 }

 catch { return null; }

 }

 ///

 /// 字节转图片

 ///

 public static Image FromBytes(byte[] bs)

 {

 if (bs == null) return null;

 try

 {

 MemoryStream ms = new MemoryStream(bs);

 Image returnImage = Image.FromStream(ms);

 ms.Close();

 return returnImage;

 }

 catch { return null; }

 }

 ///

 /// 将其它格式的图片转为JPG文件

 ///

 public static Image ToJPG(Image source)

 {

 //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+

 Bitmap bmp = new Bitmap(source);

 MemoryStream ms = new MemoryStream();

 bmp.Save(ms, ImageFormat.Jpeg);

 return Bitmap.FromStream(ms);

 }

 ///

 /// 将其它格式的图片转为PNG文件

 ///

 public static Image ToPNG(Image source)

 {

 //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+

 Bitmap bmp = new Bitmap(source);

 MemoryStream ms = new MemoryStream();

 bmp.Save(ms, ImageFormat.Png);

 return FromBytes(ms.ToArray());

 }

 //保存文件

 public static void SaveFile(string fileName, Image source)

 {

 source.Save(fileName, source.RawFormat);

 }

 }

 }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22328375/viewspace-612573/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-08-12

  • 博文量
    14
  • 访问量
    15269