1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WinMirrorImageSpeedCSharp
{
public class LockImage
{
private Bitmap BaseImage;
private int BaseImageWidth;
private int BaseImageHeight;
private int TotalPixels;
private IntPtr ImageAddress ;
private BitmapData ImageContent ;
private int[] ImageBuffer;
// ----- User access to the relevant image.
public Bitmap Image
{
get { return BaseImage; }
set
{
Graphics canvas ;
BaseImage = new Bitmap(value.Width, value.Height, value.PixelFormat);
canvas = Graphics.FromImage(BaseImage);
canvas.DrawImage(value, 0, 0, value.Width, value.Height);
canvas.Dispose();
}
}
private void LockTheImage()
{
// ----- Lock the image in memory. How much room
// do we need?
BaseImageWidth = BaseImage.Width;
BaseImageHeight = BaseImage.Height;
TotalPixels = BaseImageWidth * BaseImageHeight;
/* Create a stable (locked) area in memory. It will store 32-bit color images. */
ImageBuffer=new int [TotalPixels ];
ImageContent = BaseImage.LockBits(
new Rectangle(0, 0, BaseImageWidth,
BaseImageHeight), ImageLockMode.ReadWrite,
PixelFormat.Format32bppRgb);
ImageAddress = ImageContent.Scan0;
// ----- Associate the buffer and the locked memory.
Marshal.Copy(ImageAddress, ImageBuffer, 0, TotalPixels);
}
private void UnlockTheImage()
{
// ----- Unlock the memory area.
Marshal.Copy(ImageBuffer, 0, ImageAddress, TotalPixels);
Image.UnlockBits(ImageContent);
ImageContent = null;
ImageBuffer = new int[0];
}
// 256 shades of gray are created
public void MakeGray()
{
// ----- Make a grayscale version of the image.
int pixelIndex ;
int onePixel ;
int alphaPart ;
int redPart ;
int greenPart ;
int bluePart;
int maxColor ;
int minColor ;
int down ;
int across;
// ----- Lock the image for speed.
LockTheImage();
// ----- Process each pixel in the grid.
for( down = 0; down < BaseImageHeight ;down++)
{
for (across = 0 ;across< BaseImageWidth ;across++)
{
// ----- Locate the pixel's color.
pixelIndex = down * BaseImageWidth + across;
onePixel = ImageBuffer[pixelIndex];
// ----- Extract the color values.
alphaPart = (onePixel >> 24) & 0xFF;
redPart = (onePixel >> 16) & 0xFF;
greenPart = (onePixel >> 8) & 0xFF;
bluePart = onePixel & 0xFF;
// ----- Get the general color intensity.
maxColor = Math.Max(redPart, Math.Max(greenPart, bluePart));
minColor = Math.Min(redPart, Math.Min(greenPart, bluePart));
onePixel = (maxColor + minColor) / 2;
// ----- Use a common intensity for all colors.
bluePart = onePixel;
greenPart = onePixel;
redPart = onePixel;
// ----- Set the pixel to the new color. Retain
// the original alpha channel.
ImageBuffer[pixelIndex] = (alphaPart << 24) + (redPart << 16) + (greenPart << 8) + bluePart;
}
}
// ----- Finished. Unlock the image.
UnlockTheImage();
}
}
} |
Partager