183 lines
6.2 KiB
C#
183 lines
6.2 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace ClassLibrary
|
|
{
|
|
public class ImageProcessing
|
|
{
|
|
|
|
public bool calculateWhitePixels (string strFileName, ref int nBlackCount, ref int nWhiteCount, ref int nTotalPixels)
|
|
{
|
|
Image image = null;
|
|
Bitmap bmpCrop = null;
|
|
|
|
BitmapData bmpData = null;
|
|
byte[] imgData = null;
|
|
int n = 0;
|
|
|
|
try
|
|
{
|
|
image = new Bitmap(strFileName);
|
|
bmpCrop = new Bitmap(image);
|
|
|
|
//nBlackPercent = 0;
|
|
//nWhitePercent = 0;
|
|
|
|
for (int h = 0; h < bmpCrop.Height; h++)// scan through all rows of image
|
|
{
|
|
// This part is a very fast replacement of getPixel() method
|
|
bmpData = bmpCrop.LockBits(new Rectangle(0, h, bmpCrop.Width, 1),
|
|
ImageLockMode.ReadOnly, image.PixelFormat);
|
|
imgData = new byte[bmpData.Stride];
|
|
Marshal.Copy(bmpData.Scan0, imgData, 0, imgData.Length);
|
|
bmpCrop.UnlockBits(bmpData);
|
|
|
|
for (n = 0; n < imgData.Length - 3; n += 3)// scanning width of image
|
|
{
|
|
if ((int)imgData[n] == 0 && (int)imgData[n + 1] == 0 && (int)imgData[n + 2] == 0)// Color black is #000000
|
|
{
|
|
nBlackCount++;
|
|
}
|
|
else
|
|
{
|
|
nWhiteCount++;
|
|
}
|
|
}
|
|
|
|
nTotalPixels += n;
|
|
}
|
|
|
|
string.Format("{0} pixels are black, {1} are white(or non black at least).", nBlackCount, nWhiteCount);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
|
|
return nWhiteCount > 0;
|
|
}
|
|
|
|
public static unsafe bool DetectColorAllBlack(Bitmap bmp)
|
|
{
|
|
// Lock the bitmap's bits.
|
|
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
|
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
|
|
|
|
// Get the address of the first line.
|
|
IntPtr ptr = bmpData.Scan0;
|
|
|
|
// Declare an array to hold the bytes of the bitmap.
|
|
int bytes = bmpData.Stride * bmp.Height;
|
|
byte[] rgbValues = new byte[bytes];
|
|
|
|
// Copy the RGB values into the array.
|
|
Marshal.Copy(ptr, rgbValues, 0, bytes);
|
|
|
|
// Scanning for non-zero bytes
|
|
for (int index = 0; index < rgbValues.Length; index++)
|
|
if (rgbValues[index] != 0 && (index + 1) % 4 != 0)
|
|
return false;
|
|
// Unlock the bits.
|
|
bmp.UnlockBits(bmpData);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
|
|
{
|
|
System.Drawing.Bitmap bmpOut = null;
|
|
try
|
|
{
|
|
Bitmap loBMP = new Bitmap(lcFilename);
|
|
ImageFormat loFormat = loBMP.RawFormat;
|
|
|
|
decimal lnRatio;
|
|
int lnNewWidth = 0;
|
|
int lnNewHeight = 0;
|
|
|
|
//*** If the image is smaller than a thumbnail just return it
|
|
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
|
|
return loBMP;
|
|
|
|
if (loBMP.Width > loBMP.Height)
|
|
{
|
|
lnRatio = (decimal)lnWidth / loBMP.Width;
|
|
lnNewWidth = lnWidth;
|
|
decimal lnTemp = loBMP.Height * lnRatio;
|
|
lnNewHeight = (int)lnTemp;
|
|
}
|
|
else
|
|
{
|
|
lnRatio = (decimal)lnHeight / loBMP.Height;
|
|
lnNewHeight = lnHeight;
|
|
decimal lnTemp = loBMP.Width * lnRatio;
|
|
lnNewWidth = (int)lnTemp;
|
|
}
|
|
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
|
|
Graphics g = Graphics.FromImage(bmpOut);
|
|
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
|
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
|
|
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
|
|
|
|
loBMP.Dispose();
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return bmpOut;
|
|
}
|
|
|
|
public static Bitmap CreateThumbnail(Bitmap loBMP, int lnWidth, int lnHeight)
|
|
{
|
|
System.Drawing.Bitmap bmpOut = null;
|
|
try
|
|
{
|
|
ImageFormat loFormat = loBMP.RawFormat;
|
|
|
|
decimal lnRatio;
|
|
int lnNewWidth = 0;
|
|
int lnNewHeight = 0;
|
|
|
|
//*** If the image is smaller than a thumbnail just return it
|
|
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
|
|
return loBMP;
|
|
|
|
if (loBMP.Width > loBMP.Height)
|
|
{
|
|
lnRatio = (decimal)lnWidth / loBMP.Width;
|
|
lnNewWidth = lnWidth;
|
|
decimal lnTemp = loBMP.Height * lnRatio;
|
|
lnNewHeight = (int)lnTemp;
|
|
}
|
|
else
|
|
{
|
|
lnRatio = (decimal)lnHeight / loBMP.Height;
|
|
lnNewHeight = lnHeight;
|
|
decimal lnTemp = loBMP.Width * lnRatio;
|
|
lnNewWidth = (int)lnTemp;
|
|
}
|
|
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
|
|
Graphics g = Graphics.FromImage(bmpOut);
|
|
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
|
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
|
|
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
|
|
|
|
loBMP.Dispose();
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return bmpOut;
|
|
}
|
|
|
|
|
|
}
|
|
}
|