
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
| 青色(B)の8ビット | 緑色(G)の8ビット | 赤色(R)の8ビット | Alpha(A)の8ビット |
using System.Runtime.InteropServices; using System.Diagnostics; |
[STAThread]
public static void Main()
|
[DllImport("gdi32.dll")]
private static extern int BitBlt(IntPtr hDestDC,
int x, int y, int nWidth, int nHeight,
IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
[DllImport("User32.Dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.Dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern int SelectObject(IntPtr hdc, IntPtr hbmp);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFO bi, UInt32 iUsage,
IntPtr ppvBits, IntPtr hSection, UInt32 dwOffset);
[DllImport("gdi32.dll")]
public static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, UInt32 uStartScan, UInt32 cScanLines,
IntPtr lpvBits, IntPtr lpbi, UInt32 uUsage);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hdc);
|

//☆ Face Object Class unsafe class Face |
public Bitmap m_bmp; // 入力画像
public IntPtr m_Dib; // DIB 画像
public IntPtr m_DibDC; // DIB DC
public byte *dat; // DIB ピクセルデータ
public const int BI_RGB = 0;
public const int DIB_RGB_COLORS = 0;
public const int SRCCOPY = 0xcc0020;
|
struct BITMAPINFOHEADER
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public UInt16 biPlanes;
public UInt16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
};
unsafe struct BITMAPINFO
{
public BITMAPINFOHEADER bmih;
public fixed byte bmiColors[1];
};
//☆ DIB の初期化
unsafe public void InitDib()
{
IntPtr hdc = GetDC(IntPtr.Zero);
BITMAPINFO bi = new BITMAPINFO(); // BITMAP 構造体
bi.bmih.biSize = (UInt32)sizeof(BITMAPINFOHEADER);
bi.bmih.biPlanes = (UInt16)1;
bi.bmih.biCompression = BI_RGB;
bi.bmih.biBitCount = 32;
bi.bmih.biWidth = m_bmp.Width;
bi.bmih.biHeight = m_bmp.Height;
fixed (void* ppvBits = &dat)
{
m_Dib = CreateDIBSection(hdc, bi, 0, (IntPtr)ppvBits, (IntPtr)null, 0);
if (m_Dib == null) MessageBox.Show("CreateDIBSection error");
}
m_DibDC = CreateCompatibleDC(hdc);
SelectObject(m_DibDC, m_Dib);
ReleaseDC(IntPtr.Zero, hdc);
}
|
// byte *dat; の印字確認
public void ChkPix()
{
for(int i = 0; i < 32; i++)
{ DebugRGB(dat+i*4); }
}
public void DebugRGB(byte *v)
{
Debug.Write("BGRA: " + *v + ", " + *(v+1) + ", " + *(v+2) + ", " + *(v+3) + "\n");
}
|
![]()
bmp = new Bitmap(ImgFile);
Width = bmp.Width;
Height = bmp.Height+32;
|
App = new Face();
float hdpi = App.bmp.HorizontalResolution;
float vdpi = App.bmp.VerticalResolution;
Width = App.bmp.Width*96/(int)hdpi;
Height = (App.bmp.Height+32)*96/(int)vdpi;
Debug.Write("Width:" + App.bmp.Width + " Hight:" + App.bmp.Height + " H:" + hdpi + " V:" + vdpi + "\n");
|
public void View(Graphics g, int x, int y)
{ if (bmp != null) g.DrawImage(bmp, x, y);
}
|
public void ViewDib(Graphics g, int x, int y)
{ if (m_DibDC == null) return;
IntPtr hDC = g.GetHdc();
BitBlt(hDC, x, y, m_bmp.Width, m_bmp.Height, m_DibDC, 0, 0, SRCCOPY);
g.ReleaseHdc(hDC);
}
|
![]()
![]()
[Next Chapter ↓] Face Image Size