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
| using System.Drawing;
using System.Drawing.Imaging;
public class BitmapFast
{
#region private
private byte[] b;
private int i_Width;
private int i_Height;
private int i_Stride;
private int i_ColorSize;
#endregion
#region public
public bool Disposed;
#endregion
#region New
public BitmapFast(string filename)
{
if ((filename == "") | (System.IO.File.Exists(filename) == false)) {
MsgBox("file '" + filename + "' not found!");
return;
}
Bitmap pv_bitmap = null;
pv_bitmap = new Bitmap(filename);
i_Width = pv_bitmap.Width;
i_Height = pv_bitmap.Height;
Rectangle bounds = new Rectangle(0, 0, i_Width, i_Height);
BitmapData bitmapData = pv_bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
i_stride = bitmapData.Stride;
i_ColorSize = bitmapData.Stride / bitmapData.Width;
// ERROR: Not supported in C#: ReDimStatement
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, b, 0, b.Length - 1);
pv_bitmap.UnlockBits(bitmapData);
pv_bitmap.Dispose();
pv_bitmap = null;
}
#endregion
#region Function
public Color Getpixel(int x, int y)
{
byte red;
byte green;
byte blue;
red = b(x * i_ColorSize + y * i_Stride);
green = b((x * i_ColorSize + 1) + y * i_Stride);
blue = b((x * i_ColorSize + 2) + y * i_Stride);
return Color.FromArgb(255, red, green, blue);
}
public void Setpixel(int x, int y, Color color)
{
b(x * i_ColorSize + y * i_Stride) = color.R;
b((x * i_ColorSize + 1) + y * i_Stride) = color.G;
b((x * i_ColorSize + 2) + y * i_Stride) = color.B;
}
#endregion
#region Save
public void Save(string filename, ImageFormat myFormat)
{
Bitmap pv_bitmap = null;
pv_bitmap = new Bitmap(i_Width, i_Height, PixelFormat.Format24bppRgb);
Rectangle bounds = new Rectangle(0, 0, i_Width, i_Height);
Imaging.BitmapData bitmapData = pv_bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length - 1);
pv_bitmap.Save(filename, myFormat);
pv_bitmap.UnlockBits(bitmapData);
pv_bitmap.Dispose();
pv_bitmap = null;
}
#endregion
#region GetBitmap
public Bitmap Get_Bitmap()
{
Get_Bitmap = new Bitmap(i_Width, i_Height, PixelFormat.Format24bppRgb);
Rectangle bounds = new Rectangle(0, 0, i_Width, i_Height);
Imaging.BitmapData bitmapData = Get_Bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
bitmapData.Stride = (int)bitmapData.Stride / i_Width * i_Width;
System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length - 1);
Get_Bitmap.UnlockBits(bitmapData);
}
#endregion
#region property
public int Width {
get { return i_Width; }
}
public int Height {
get { return i_Height; }
}
#endregion
#region dispose
public void Dispose()
{
if (Disposed == true) return;
b = null;
Disposed = true;
}
protected override void Finalize()
{
base.Finalize();
if (Disposed == false) Dispose();
}
#endregion
} |
Partager