📄 drawingwrapper.cs.svn-base
字号:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace Aspecto
{
public class BITMAPINFO
{
public Int32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public Int32 biCompression;
public Int32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public Int32 biClrUsed;
public Int32 biClrImportant;
};
public class DrawingWrapper
{
internal const int SRCCOPY = 0xCC0020;
[System.Runtime.InteropServices.DllImport ("coredll.dll", EntryPoint="CreateDIBSection")]
internal static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFO pbmi, uint iUsage, out IntPtr
ppvBits, IntPtr hSection, uint dwOffset);
[System.Runtime.InteropServices.DllImport ("coredll.dll", EntryPoint="DeleteDC")]
internal static extern IntPtr DeleteDC( IntPtr hdc );
[System.Runtime.InteropServices.DllImport ("coredll.dll", EntryPoint="CreateCompatibleDC")]
internal static extern IntPtr CreateCompatibleDC( IntPtr hdc );
[System.Runtime.InteropServices.DllImport ("coredll.dll", EntryPoint="SelectObject")]
internal static extern IntPtr SelectObject( IntPtr hdc, IntPtr hgdiobj );
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll", EntryPoint="GetDC")]
internal static extern IntPtr GetDC( IntPtr hWnd);
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll", EntryPoint="GetWindowDC")]
internal static extern IntPtr GetWindowDC( IntPtr hWnd);
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll", EntryPoint="ReleaseDC")]
internal static extern IntPtr ReleaseDC( IntPtr hWnd, IntPtr hDC );
[DllImport("coredll")]
extern static public bool BitBlt(IntPtr dest, int xdest, int ydest, int width, int height,
IntPtr src, int xsrc, int ysrc, uint op);
[DllImport("coredll")]
extern static public bool PatBlt(
IntPtr hdc,
int nXLeft,
int nYLeft,
int nWidth,
int nHeight,
uint dwRop
);
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll")]
public static extern bool StretchBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle, negative to switch upside down
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
int nWidthSrc,
int nHeightSrc,
uint dwRop // raster operation code
);
[DllImport("coredll.dll", EntryPoint="LocalAlloc")]
internal static extern IntPtr LocalAlloc(uint flags, int cb);
[DllImport("coredll.dll", EntryPoint="LocalFree")]
internal static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("coredll", EntryPoint="GetForegroundWindow")]
internal static extern IntPtr GetForegroundWindow();
public static byte[] CreateBitmap(int width, int height, byte[] bitmapData, BITMAPINFO bi)
{
const int sizeBFH = 14; //sizeof(BITMAPFILEHEADER)
if ( (width & 1) == 1 )
throw new ArgumentException("Width must be an even number");
int nSize = sizeBFH + Marshal.SizeOf(typeof(BITMAPINFO)) + (width << 3) * height;
byte[] data = new byte[nSize];
byte[] bfh = new byte[sizeBFH];
BitConverter.GetBytes(( short )0x4d42).CopyTo( data, 0 );
BitConverter.GetBytes( nSize ).CopyTo( data, 2 );
int bfhOffBits = (int)(sizeBFH + Marshal.SizeOf(typeof(BITMAPINFO)));
BitConverter.GetBytes(bfhOffBits).CopyTo(data, 10);
byte[] hdr = GetBytes(bi);
Buffer.BlockCopy( hdr, 0, data, sizeBFH, hdr.Length);
Buffer.BlockCopy( bitmapData, 0, data, (int)bfhOffBits, Math.Min(bitmapData.Length, bi.biSizeImage ));
return data;
}
public static short MakeRGB555(byte r,byte g, byte b)
{
return (short)((((byte)(r) >> 3) << 10) | (((byte)(g) >> 3) << 5) | ((byte)(b) >> 3));
}
public unsafe static void MakeRGB555(ref byte r, ref byte g, ref byte b, ref short mc)
{
mc = (short)((((byte)(r) >> 3) << 10) | (((byte)(g) >> 3) << 5) | ((byte)(b) >> 3));
}
public static short MakeRGB555(Color c)
{
return MakeRGB555(c.R, c.G, c.B);
}
public static Color FromRGB555(short s)
{
byte[] c = FromRGB555ToBytes(s);
return Color.FromArgb(c[0], c[1], c[2]);
}
public static byte[] FromRGB555ToBytes(short s)
{
byte[] ret = new byte[3];
ret[0] = (byte)(((s ) >> 10) << 3);
ret[1] = (byte)(((s ) >> 5) << 3);
ret[2] = (byte)((s ) << 3);
return ret;
}
public static unsafe void FromRGB555ToBytes(ref short s, ref byte[] b)
{
b[0] = (byte)((s >> 10) << 3);
b[1] = (byte)((s >> 5) << 3);
b[2] = (byte)(s << 3);
}
public static int[] FromRGB555ToInts(short s)
{
int[] ret = new int[3];
ret[0] = (int)((byte)((s >> 10) << 3));
ret[1] = (int)((byte)((s >> 5) << 3));
ret[2] = (int)((byte)(s << 3));
return ret;
}
private static byte[] GetBytes(object o)
{
const int GPTR = 0x40;
int size = Marshal.SizeOf(o.GetType());
IntPtr p = DrawingWrapper.LocalAlloc(GPTR, size);
Marshal.StructureToPtr(o, p, false);
byte[] ret = new byte[size];
Marshal.Copy(p, ret, 0, size);
DrawingWrapper.LocalFree(p);
return ret;
}
public static BITMAPINFO GetOurNiceBitmap(int width, int height)
{
BITMAPINFO bmi = new BITMAPINFO();
bmi.biSize = Marshal.SizeOf(bmi);
bmi.biBitCount = 24;
bmi.biPlanes = 1;
bmi.biWidth = width;
bmi.biHeight = height;
bmi.biXPelsPerMeter = 0xb12;
bmi.biYPelsPerMeter = 0xb12;
bmi.biSizeImage = bmi.biWidth * bmi.biHeight * bmi.biBitCount / 8;
bmi.biClrUsed = 0;
bmi.biClrImportant = 0;
bmi.biCompression = 0;
return bmi;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -