public virtual void Create(IntPtr hDC, Int32 width, Int32 height, Int32 bitCount) { _width = width; _height = height; _parentDC = hDC; // Destroy existing objects. Destroy(); // Create a bitmap info structure. var info = new BitMapInfo(); info.Init(); // Set the data. info.biBitCount = (Int16)bitCount; info.biPlanes = 1; info.biWidth = _width; info.biHeight = _height; // Create the bitmap. _hBitmap = Native.CreateDIBSection(hDC, ref info, 0, out _bits, IntPtr.Zero, 0); Native.SelectObject(hDC, _hBitmap); // Set the OpenGL pixel format. SetPixelFormat(hDC, bitCount); }
static void Main(string[] args) { Bitmap bmpobj_src = (Bitmap)Image.FromFile("D:\\Myself\\Dataset\\Rice\\E3.jpg"); Bitmap bmpobj_dst = (Bitmap)Image.FromFile("D:\\Myself\\Dataset\\seed\\D3.jpg"); int width = bmpobj_dst.Width; int height = bmpobj_dst.Height; BitMapInfo bi_src = bitmapToByteArray(bmpobj_src, 0); BitMapInfo bi_dst = bitmapToByteArray(bmpobj_dst, 1); byte[] pfinal_pic = new byte[width*height*4]; int res = callAPI.getImage(bi_src.Result, bmpobj_src.Width, bmpobj_src.Height, bi_src.Step, bi_dst.Result, bmpobj_dst.Width, bmpobj_dst.Height, bi_dst.Step, pfinal_pic); GCHandle hObject = GCHandle.Alloc(pfinal_pic, GCHandleType.Pinned); IntPtr pBGRAData = hObject.AddrOfPinnedObject(); Bitmap SeedSizeImg = new Bitmap(width, height, width * 4, PixelFormat.Format32bppArgb, pBGRAData); if (hObject.IsAllocated) hObject.Free(); SeedSizeImg.Save("D:\\Myself\\Dataset\\7.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); bmpobj_src.Dispose(); // 回收bitmap的内存 bmpobj_dst.Dispose(); bmpobj_src = null; bmpobj_dst = null; }
/// <summary> /// Resizes the section. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitCount">The bit count.</param> public void Resize(Int32 width, Int32 height, Int32 bitCount) { // Destroy existing objects. Destroy(); // Set parameters. Width = width; Height = height; // Create a bitmap info structure. var info = new BitMapInfo(); info.Init(); // Set the data. info.biBitCount = (Int16)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. _hBitmap = Native.CreateDIBSection(_parentDC, ref info, 0, out _bits, IntPtr.Zero, 0); Native.SelectObject(_parentDC, _hBitmap); }
public static BitMapInfo bitmapToByteArray(Bitmap src, int flag) { byte[] result; int step; int iWidth = src.Width; int iHeight = src.Height; Rectangle rect = new Rectangle(0, 0, iWidth, iHeight); BitmapData bmpData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat); IntPtr iPtr = bmpData.Scan0; step = bmpData.Stride; int iBytes = iHeight * step; byte[] pixelValues = new byte[iBytes]; Marshal.Copy(iPtr, pixelValues, 0, iBytes); src.UnlockBits(bmpData); byte[] realValues; int offset; if (flag == 1) { realValues = new byte[iHeight * iWidth * 3]; offset = step - iWidth * 3; } else { realValues = new byte[iHeight * iWidth]; offset = step - iWidth; } int realValuesIndex = 0; int pixelValuesIndex = 0; for (int i = 0; i < iHeight; i++) { for (int j = 0; j < iWidth; j++) { if (flag == 1) { realValues[realValuesIndex++] = pixelValues[pixelValuesIndex++]; realValues[realValuesIndex++] = pixelValues[pixelValuesIndex++]; realValues[realValuesIndex++] = pixelValues[pixelValuesIndex++]; } else realValues[realValuesIndex++] = pixelValues[pixelValuesIndex++]; } pixelValuesIndex += offset; } result = realValues; BitMapInfo bi; if (flag == 1) bi = new BitMapInfo { Result = result, Step = iWidth * 3 , Stride = step}; else bi = new BitMapInfo { Result = result, Step = iWidth, Stride = step }; return bi; }
public void SendImageDelegate(object c) { TcpClient clt = c as TcpClient; if (clt != null) { var pipe = clt.GetStream(); while (true) { BitMapInfo info = new BitMapInfo(); info.size = (uint)bufferSize; info.source = buffer; if (GetBitImageByFullScreen(ref info) == 0) { if (info.size < bufferSize) { try { Marshal.Copy(buffer, imageBuffer, 0, (int)info.size); long size = info.size; long srcsize = info.src_size; byte[] ot = BitConverter.GetBytes(size); pipe.Write(ot, 0, ot.Length); ot = BitConverter.GetBytes(srcsize); pipe.Write(ot, 0, ot.Length); pipe.Write(imageBuffer, 0, (int)info.size); pipe.Flush(); int ch = pipe.ReadByte(); if (ch != 0xF) { break; } } catch (Exception) { break; } } } else { break; } } this.client.Close(); this.client = null; this.thread = null; Application.Exit(); } }
static void Main(string[] args) { DirectoryInfo dir = new DirectoryInfo("E:\\work\\picture\\test"); FileInfo[] fileInfo = dir.GetFiles(); //List<string> fileNames = new List<string>(); //int index = 0; foreach (FileInfo item in fileInfo) { Bitmap bmpobj = (Bitmap)Image.FromFile("E:\\work\\picture\\test\\" + item); BitMapInfo bi = bitmapToByteArray(bmpobj); float ratio = findCalibartionRatio(bi.Result, bmpobj.Width, bmpobj.Height, bi.Step, /*index++,*/ 300); Bitmap bitmap = byteToGrayBitmap(bi.Result, bmpobj.Width, bmpobj.Height); bmpobj.Dispose(); // 回收bitmap的内存 bmpobj = null; bitmap.Save("bitmap.jpeg", ImageFormat.Jpeg); Console.Write("E:\\work\\picture\\test\\" + item.Name + ":"); Console.WriteLine(ratio); } }
private static IntPtr CreateMemoryHdc(IntPtr hdc, int width, int height, out IntPtr dib) { // Create a memory DC so we can work off-screen IntPtr memoryHdc = CreateCompatibleDC(hdc); SetBkMode(memoryHdc, 1); // Create a device-independent bitmap and select it into our DC var info = new BitMapInfo(); info.biSize = Marshal.SizeOf(info); info.biWidth = width; info.biHeight = -height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr ppvBits; dib = CreateDIBSection(hdc, ref info, 0, out ppvBits, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); return(memoryHdc); }
/// <summary> /// Special draw logic to draw transparent text using GDI.<br/> /// 1. Create in-memory DC<br/> /// 2. Copy background to in-memory DC<br/> /// 3. Draw the text to in-memory DC<br/> /// 4. Copy the in-memory DC to the proper location with alpha blend<br/> /// </summary> public void DrawTransparentText(string str, Font font, Color color, Point point, Size size) { // Create a memory DC so we can work off-screen IntPtr memoryHdc = CreateCompatibleDC(_hdc); SetBkMode(memoryHdc, 1); // Create a device-independent bitmap and select it into our DC var info = new BitMapInfo(); info.biSize = Marshal.SizeOf(info); info.biWidth = size.Width; info.biHeight = -size.Height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr ppvBits; IntPtr dib = CreateDIBSection(_hdc, ref info, 0, out ppvBits, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); try { // copy target background to memory HDC so when copied back it will have the proper background BitBlt(memoryHdc, 0, 0, size.Width, size.Height, _hdc, point.X, point.Y, 0x00CC0020); // Create and select font SelectObject(memoryHdc, GetCachedHFont(font)); SetTextColor(memoryHdc, (color.B & 0xFF) << 16 | (color.G & 0xFF) << 8 | color.R); // Draw text to memory HDC TextOut(memoryHdc, 0, 0, str, str.Length); // copy from memory HDC to normal HDC with alpha blend so achieve the transparent text AlphaBlend(_hdc, point.X, point.Y, size.Width, size.Height, memoryHdc, 0, 0, size.Width, size.Height, new BlendFunction(color.A)); } finally { DeleteObject(dib); DeleteDC(memoryHdc); } }
private static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BitMapInfo pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
private static IntPtr CreateMemoryHdc(IntPtr hdc, int width, int height, out IntPtr dib) { // Create a memory DC so we can work off-screen IntPtr memoryHdc = CreateCompatibleDC(hdc); SetBkMode(memoryHdc, 1); // Create a device-independent bitmap and select it into our DC var info = new BitMapInfo(); info.biSize = Marshal.SizeOf(info); info.biWidth = width; info.biHeight = -height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr ppvBits; dib = CreateDIBSection(hdc, ref info, 0, out ppvBits, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); return memoryHdc; }
private void DrawTransparentText(IntPtr fontHandle, string str, Color color, Point point, Size size, TextAlignFlags flags, bool multilineSupport) { // Create a memory DC so we can work off-screen IntPtr memoryHdc = CreateCompatibleDC(_hdc); SetBkMode(memoryHdc, 1); // Create a device-independent bitmap and select it into our DC var info = new BitMapInfo(); info.biSize = Marshal.SizeOf(info); info.biWidth = size.Width; info.biHeight = -size.Height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr ppvBits; IntPtr dib = CreateDIBSection(_hdc, ref info, 0, out ppvBits, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); try { // copy target background to memory HDC so when copied back it will have the proper background BitBlt(memoryHdc, 0, 0, size.Width, size.Height, _hdc, point.X, point.Y, 0x00CC0020); // Create and select font SelectObject(memoryHdc, fontHandle); SetTextColor(memoryHdc, (color.B & 0xFF) << 16 | (color.G & 0xFF) << 8 | color.R); Size strSize = new Size(); Point pos = new Point(); if (multilineSupport) { TextFormatFlags fmtFlags = TextFormatFlags.WordBreak; // Aligment if (flags.HasFlag(TextAlignFlags.Center)) { fmtFlags |= TextFormatFlags.Center; } if (flags.HasFlag(TextAlignFlags.Right)) { fmtFlags |= TextFormatFlags.Right; } // Calculate the string size Rect strRect = new Rect(new Rectangle(point, size)); DrawText(memoryHdc, str, str.Length, ref strRect, TextFormatFlags.CalcRect | fmtFlags); if (flags.HasFlag(TextAlignFlags.Middle)) { pos.Y = ((size.Height) >> 1) - (strRect.Height >> 1); } if (flags.HasFlag(TextAlignFlags.Bottom)) { pos.Y = (size.Height) - (strRect.Height); } // Draw Text for multiline format Rect region = new Rect(new Rectangle(pos, size)); DrawText(memoryHdc, str, str.Length, ref region, fmtFlags); } else { // Calculate the string size GetTextExtentPoint32(memoryHdc, str, str.Length, ref strSize); // Aligment if (flags.HasFlag(TextAlignFlags.Center)) { pos.X = ((size.Width) >> 1) - (strSize.Width >> 1); } if (flags.HasFlag(TextAlignFlags.Right)) { pos.X = (size.Width) - (strSize.Width); } if (flags.HasFlag(TextAlignFlags.Middle)) { pos.Y = ((size.Height) >> 1) - (strSize.Height >> 1); } if (flags.HasFlag(TextAlignFlags.Bottom)) { pos.Y = (size.Height) - (strSize.Height); } // Draw text to memory HDC TextOut(memoryHdc, pos.X, pos.Y, str, str.Length); } // copy from memory HDC to normal HDC with alpha blend so achieve the transparent text AlphaBlend(_hdc, point.X, point.Y, size.Width, size.Height, memoryHdc, 0, 0, size.Width, size.Height, new BlendFunction(color.A)); } finally { DeleteObject(dib); DeleteDC(memoryHdc); } }
private static extern int DecompressImage(ref BitMapInfo bitMap);
private static extern int GetBitImageByFullScreen(ref BitMapInfo bitMap);