Ultra-Practical Image and Practical image

Source: Internet
Author: User

Ultra-Practical Image and Practical image

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

public static class ImageHelper
{
private static float[][] ColorMatrix = null;

static ImageHelper()
{
float[][] numArray = new float[5][];
numArray[0] = new float[] { 0.299f, 0.299f, 0.299f, 0f, 0f };
numArray[1] = new float[] { 0.587f, 0.587f, 0.587f, 0f, 0f };
numArray[2] = new float[] { 0.114f, 0.114f, 0.114f, 0f, 0f };
float[] numArray2 = new float[5];
numArray2[3] = 1f;
numArray[3] = numArray2;
numArray2 = new float[5];
numArray2[4] = 1f;
numArray[4] = numArray2;
ColorMatrix = numArray;
}

public static Bitmap ConstructRGB24Bitmap(byte[] coreData, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(coreData, 0, bitmapdata.Scan0, coreData.Length);
bitmap.UnlockBits(bitmapdata);
return bitmap;
}

public static Image Convert(byte[] buff)
{
MemoryStream stream = new MemoryStream(buff);
Image image = Image.FromStream(stream);
stream.Close();
return image;
}

public static byte[] Convert(Image img)
{
Image image = CopyImageDeeply(img);
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
byte[] buffer = stream.ToArray();
stream.Close();
image.Dispose();
return buffer;
}

public static Bitmap ConvertToGrey(Image origin)
{
Bitmap image = new Bitmap(origin);
Graphics graphics = Graphics.FromImage(image);
ImageAttributes imageAttr = new ImageAttributes();
System.Drawing.Imaging.ColorMatrix newColorMatrix = new System.Drawing.Imaging.ColorMatrix(ColorMatrix);
imageAttr.SetColorMatrix(newColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
graphics.Dispose();
return image;
}

public static Icon ConvertToIcon(Image img, int iconLength)
{
using (Bitmap bitmap = new Bitmap(img, new Size(iconLength, iconLength)))
{
return Icon.FromHandle(bitmap.GetHicon());
}
}

public static Image ConvertToJPG(Image img)
{
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
Image image = Image.FromStream(stream);
stream.Close();
return image;
}

public static Image CopyImageDeeply(Image img)
{
Bitmap image = new Bitmap(img.Width, img.Height, img.PixelFormat);
Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
graphics.Dispose();
return image;
}

public static byte[] GetRGB24CoreData(Bitmap bm)
{
byte[] destination = new byte[(bm.Width * bm.Height) * 3];
BitmapData bitmapdata = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(bitmapdata.Scan0, destination, 0, destination.Length);
bm.UnlockBits(bitmapdata);
return destination;
}

public static bool IsGif(Image img)
{
FrameDimension dimension = new FrameDimension(img.FrameDimensionsList[0]);
return (img.GetFrameCount(dimension) > 1);
}

public static byte[] ReviseRGB24Data(byte[] origin, Size originSize, Size newSize)
{
Bitmap image = ConstructRGB24Bitmap(origin, originSize.Width, originSize.Height);
Bitmap bitmap2 = new Bitmap(newSize.Width, newSize.Height);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.DrawImage(image, 0f, 0f, new RectangleF(0f, 0f, (float) newSize.Width, (float) newSize.Height), GraphicsUnit.Pixel);
graphics.Dispose();
return GetRGB24CoreData(bitmap2);
}

public static void Save(Image img, string path, ImageFormat format)
{
if ((img != null) && (path != null))
{
CopyImageDeeply(img).Save(path, format);
}
}
}

 

Retrieve the image field from the database:

HeadImageData =  dr["HeadImageData"] as byte[] ?? null

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.