using System; using System.Drawing; using System.Drawing.Imaging; using System.Security.Permissions; namespace LYFZ.OtherExpansion { public static class BitmapHelper { //[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] // public unsafe static Color GetImageAverageColor(Bitmap bitmap) public static Color GetImageAverageColor(Bitmap bitmap) { if (bitmap == null) { throw new ArgumentNullException("bitmap"); } int width = bitmap.Width; int height = bitmap.Height; Rectangle rect = new Rectangle(0, 0, width, height); Color result; try { BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); //byte* scan0 = (byte*)((void*)bitmapData.Scan0); int strideOffset = bitmapData.Stride - bitmapData.Width * 4; int sum = width * height; int a = 0; int r = 0; int g = 0; int b = 0; for (int i = 0; i < height; i++) { //for (int j = 0; j < width; j++) //{ // int arg_79_0 = b; // byte* expr_72 = scan0; // scan0 = expr_72 + 1 / 1; // b = arg_79_0 + (int)(*expr_72); // int arg_87_0 = g; // byte* expr_80 = scan0; // scan0 = expr_80 + 1 / 1; // g = arg_87_0 + (int)(*expr_80); // int arg_95_0 = r; // byte* expr_8E = scan0; // scan0 = expr_8E + 1 / 1; // r = arg_95_0 + (int)(*expr_8E); // int arg_A3_0 = a; // byte* expr_9C = scan0; // scan0 = expr_9C + 1 / 1; // a = arg_A3_0 + (int)(*expr_9C); //} //scan0 += strideOffset / 1; } bitmap.UnlockBits(bitmapData); a /= sum; r /= sum; g /= sum; b /= sum; result = Color.FromArgb(255, r, g, b); } catch { result = Color.FromArgb(127, 127, 127); } return result; } } }