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 120 121 122 123 124 125 126 127 128 129
|
using System;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WinDecouperBmp
{
public class LockImage
{
private Bitmap BaseImage ;
private int BaseImageWidth ;
private int BaseImageHeight ;
private int TotalPixels ;
private IntPtr ImageAddress ;
private BitmapData ImageContent;
private int[] ImageBuffer;
//'comptage pixels
public int CountLeftPixels { get; set; }
public int CountRightPixels { get; set; }
public Bitmap Image
{
// User access to the relevant 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];
}
public void Colorize(AxisVertical axe,Color rightColor,Color leftColor)
{
int pixelIndex ;
int onePixel ;
int alphaPart ;
int redPart ;
int greenPart ;
int bluePart ;
//----- Lock the image for speed.
LockTheImage();
//----- Process each pixel in the grid.
for (int y = 0 ; y < BaseImageHeight; y++)
{
for (int x = 0 ; x< BaseImageWidth; x++)
{
// ----- Locate the pixel's color.
pixelIndex = y * BaseImageWidth + x;
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.
if ( x<= axe.Point1.X)
{
CountLeftPixels++;
redPart = rightColor.R;
greenPart = rightColor.G ;
bluePart = rightColor.B ;
}
else if ( x > axe.Point1.X)
{
CountRightPixels++;
redPart = leftColor .R;
greenPart = leftColor.G;
bluePart = leftColor.B;
}
// ----- 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