Sylloge
A C# helper library
code/Data/Imaging.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 
00005 namespace Sylloge.Data
00006 {
00010     public static class Imaging
00011     {
00017         public static System.Drawing.Image FromFile(string file)
00018         {
00019             if (System.IO.File.Exists(file)) { return System.Drawing.Image.FromFile(file); }
00020             return null;
00021         }
00022 
00028         public static System.Drawing.Image FromStream(System.IO.Stream stream)
00029         {
00030             if (stream != null && stream.Length > 0) {
00031                 return System.Drawing.Image.FromStream(stream);
00032             }
00033             return null;
00034         }
00035 
00041         public static string GetMimeType(System.Drawing.Image img)
00042         {
00043             foreach (System.Drawing.Imaging.ImageCodecInfo c in System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()) {
00044                 if (c.FormatID == img.RawFormat.Guid) { return c.MimeType; }
00045             }
00046             return "image/unknown";
00047         }
00048 
00055         public static byte[] ToByteArray(System.Drawing.Image img, string mimeType)
00056         {
00057             System.Drawing.Imaging.ImageFormat fmt = new System.Drawing.Imaging.ImageFormat(Guid.Empty);
00058             mimeType = mimeType.ToLower();
00059             if (mimeType.Contains("jpg") || mimeType.Contains("jpeg")) {
00060                 fmt = System.Drawing.Imaging.ImageFormat.Jpeg;
00061             } else if (mimeType.Contains("bitmap") || mimeType.Contains("bmp")) {
00062                 fmt = System.Drawing.Imaging.ImageFormat.Bmp;
00063             } else if (mimeType.Contains("icon") || mimeType.Contains("ico")) {
00064                 fmt = System.Drawing.Imaging.ImageFormat.Icon;
00065             } else if (mimeType.Contains("png")) {
00066                 fmt = System.Drawing.Imaging.ImageFormat.Png;
00067             } else if (mimeType.Contains("gif")) {
00068                 fmt = System.Drawing.Imaging.ImageFormat.Gif;
00069             } else if (mimeType.Contains("tiff")) {
00070                 fmt = System.Drawing.Imaging.ImageFormat.Tiff;
00071             } else {
00072                 fmt = System.Drawing.Imaging.ImageFormat.Jpeg;
00073             }
00074             System.IO.MemoryStream istream = new System.IO.MemoryStream();
00075             img.Save(istream, fmt);
00076             istream.Seek(0, System.IO.SeekOrigin.Begin);
00077             return istream.ToArray();
00078         }
00079 
00086         public static byte[] ToByteArray(System.Drawing.Image img, System.Drawing.Imaging.ImageFormat format)
00087         {
00088             if (img == null) { return new byte[] { }; }
00089             System.IO.MemoryStream istream = new System.IO.MemoryStream();
00090             img.Save(istream, format);
00091             istream.Seek(0, System.IO.SeekOrigin.Begin);
00092             return istream.ToArray();
00093         }
00094     }
00095 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events