1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Security.Permissions;
- namespace LYFZ.OtherExpansion
- {
- public static class BitmapHelper
- {
-
-
- 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);
-
- 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++)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- 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;
- }
- }
- }
|