public unsafe byte[] CreateIconGroupData(uint nBaseID) { byte[] data = new byte[SizeOfIconGroupData()]; fixed(ICONDIR *ptr = &_iconDir) { Marshal.Copy((IntPtr)ptr, data, 0, sizeof(ICONDIR)); } int offset = sizeof(ICONDIR); for (int i = 0; i < GetImageCount(); i++) { GRPICONDIRENTRY grpEntry = new GRPICONDIRENTRY(); BITMAPINFOHEADER bitmapheader = new BITMAPINFOHEADER(); BITMAPINFOHEADER *ptr = &bitmapheader; { Marshal.Copy(GetImageData(i), 0, (IntPtr)ptr, sizeof(BITMAPINFOHEADER)); } grpEntry.bWidth = ((ICONDIRENTRY)_iconEntry[i]).bWidth; grpEntry.bHeight = ((ICONDIRENTRY)_iconEntry[i]).bHeight; grpEntry.bColorCount = ((ICONDIRENTRY)_iconEntry[i]).bColorCount; grpEntry.bReserved = ((ICONDIRENTRY)_iconEntry[i]).bReserved; grpEntry.wPlanes = bitmapheader.biPlanes; grpEntry.wBitCount = bitmapheader.biBitCount; grpEntry.dwBytesInRes = ((ICONDIRENTRY)_iconEntry[i]).dwBytesInRes; grpEntry.nID = (ushort)(nBaseID + i); GRPICONDIRENTRY *ptr2 = &grpEntry; { Marshal.Copy((IntPtr)ptr2, data, offset, Marshal.SizeOf(grpEntry)); } offset += sizeof(GRPICONDIRENTRY); } return(data); }
// メソッド public static unsafe Bitmap ToBitmap(IntPtr pBITMAPINFOHEADER) { BITMAPFILEHEADER bitmapfileheader; BITMAPINFOHEADER *bitmapinfoheaderPtr = (BITMAPINFOHEADER *)pBITMAPINFOHEADER; bitmapfileheader.bfType = 0x4d42; bitmapfileheader.bfOffBits = (uint)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)); bitmapfileheader.bfSize = bitmapfileheader.bfOffBits + bitmapinfoheaderPtr->biSizeImage画像イメージのサイズ; MemoryStream output = new MemoryStream(); BinaryWriter writer = new BinaryWriter(output); byte[] destination = new byte[sizeof(BITMAPFILEHEADER)]; Marshal.Copy((IntPtr)(&bitmapfileheader), destination, 0, destination.Length); writer.Write(destination); destination = new byte[sizeof(BITMAPINFOHEADER)]; Marshal.Copy(pBITMAPINFOHEADER, destination, 0, destination.Length); writer.Write(destination); destination = new byte[bitmapinfoheaderPtr->biSizeImage画像イメージのサイズ]; bitmapinfoheaderPtr++; Marshal.Copy((IntPtr)bitmapinfoheaderPtr, destination, 0, destination.Length); writer.Write(destination); writer.Flush(); writer.BaseStream.Position = 0L; return(new Bitmap(writer.BaseStream)); }
CreateDIBitmap ( IntPtr IntPtr_HDC, BITMAPINFOHEADER *lpbmih, uint fdwInit, byte[] dataInit, BITMAPINFO *lpbmi, uint fuUsage );
/// <summary> /// Creates a FreeImage DIB from a Device Context/Compatible Bitmap. /// </summary> /// <param name="hbitmap">Handle to the bitmap.</param> /// <param name="hdc">Handle to a device context.</param> /// <returns>Handle to a FreeImage bitmap.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="hbitmap"/> is null.</exception> public unsafe static FIBITMAP CreateFromHbitmap(IntPtr hbitmap, IntPtr hdc) { if (hbitmap == IntPtr.Zero) { throw new ArgumentNullException("hbitmap"); } FIBITMAP dib = new FIBITMAP(); BITMAP bm; uint colors; bool release; if (Gdi.GetObject(hbitmap, sizeof(BITMAP), (IntPtr)(&bm)) != 0) { dib = Allocate(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel, 0, 0, 0); if (!dib.IsNull) { colors = GetColorsUsed(dib); if (release = (hdc == IntPtr.Zero)) { hdc = Gdi.GetDC(IntPtr.Zero); } if (Gdi.GetDIBits( hdc, hbitmap, 0, (uint)bm.bmHeight, GetBits(dib), GetInfo(dib), DIB_RGB_COLORS) != 0) { if (colors != 0) { BITMAPINFOHEADER *bmih = (BITMAPINFOHEADER *)GetInfo(dib); bmih[0].biClrImportant = bmih[0].biClrUsed = colors; } } else { UnloadEx(ref dib); } if (release) { Gdi.ReleaseDC(IntPtr.Zero, hdc); } } } return(dib); }
static unsafe extern int StretchDIBits( System.IntPtr hdc, int x, int y, int dx, int dy, int SrcX, int SrcY, int wSrcWidth, int wSrcHeight, byte *lpBits, BITMAPINFOHEADER *lpBitsInfo, int wUsage, int dwRop);
private unsafe bool processVersion_Icon(IMAGE_RESOURCE_DATA_ENTRY *irdata, uint baserva, IMAGE_SECTION_HEADER *pIsh, byte *pData) { uint offset; uint ustr_offset; string sVersionInfoString; offset = irdata->OffsetToData - pIsh->VirtualAddress + pIsh->PointerToRawData; // OffsetFromRVA ICOHEADER *piHeader = (ICOHEADER *)(offset + pData); if (piHeader->imgcount > 0) { ICODATA *pIcon = (ICODATA *)(offset + pData + sizeof(ICOHEADER)); //pIcon-> BITMAPINFOHEADER *pDIP = (BITMAPINFOHEADER *)(pData + pIcon->imgadr); } return(true); }
public static Bitmap LoadImage(string file) { FileStream fs = File.OpenRead(file); byte[] buf = new byte[fs.Length]; fs.Read(buf, 0, buf.Length); IntPtr pHBInfo = IntPtr.Zero; IntPtr pHBm = IntPtr.Zero; Bitmap bmp = null; fixed(byte *p = &buf[0]) { int rc = GetPicture(p, buf.Length, 1, out pHBInfo, out pHBm, null, 0); if (rc != 0) { return(null); } BITMAPINFOHEADER *pInfo = (BITMAPINFOHEADER *)LocalLock(pHBInfo); byte * pBM = (byte *)LocalLock(pHBm); int s = 0; Rectangle r = new Rectangle(0, 0, pInfo->biWidth, pInfo->biHeight); bmp = new Bitmap(pInfo->biWidth, pInfo->biHeight, PixelFormat.Format24bppRgb); BitmapData data = bmp.LockBits(r, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); byte * dst = (byte *)data.Scan0; dst += data.Stride * data.Height; switch (pInfo->biBitCount) { case 8: break; case 16: break; case 24: for (uint y = 0; y < pInfo->biHeight; ++y) { dst -= data.Stride; for (uint x = 0; x < pInfo->biWidth; ++x) { dst[x * 3 + 0] = pBM[s++]; dst[x * 3 + 1] = pBM[s++]; dst[x * 3 + 2] = pBM[s++]; } } break; case 32: for (uint y = 0; y < pInfo->biHeight; ++y) { dst -= data.Stride; for (uint x = 0; x < pInfo->biWidth; ++x) { dst[x * 3 + 0] = pBM[s++]; dst[x * 3 + 1] = pBM[s++]; dst[x * 3 + 2] = pBM[s++]; ++s; } } break; } bmp.UnlockBits(data); LocalUnlock(pHBInfo); LocalUnlock(pHBm); return(bmp); } }
public GR.Memory.ByteBuffer CreateHDIBAsBuffer() { GR.Memory.ByteBuffer result = new GR.Memory.ByteBuffer(); BITMAPINFOHEADER bi = new BITMAPINFOHEADER(); int dwLen; IntPtr hDIB; if ((BitsPerPixel != 1) && (BitsPerPixel != 2) && (BitsPerPixel != 4) && (BitsPerPixel != 8) && (BitsPerPixel != 15) && (BitsPerPixel != 16) && (BitsPerPixel != 24) && (BitsPerPixel != 32)) { // not supported depth return(null); } bi.biSize = System.Runtime.InteropServices.Marshal.SizeOf(bi); bi.biWidth = Width; bi.biHeight = Height; bi.biPlanes = 1; bi.biBitCount = (short)BitsPerPixel; if (bi.biBitCount == 15) { bi.biBitCount = 16; } bi.biCompression = (int)BitmapCompression.BI_RGB; bi.biSizeImage = (int)(((((uint)bi.biWidth * bi.biBitCount) + 31) / 32 * 4) * bi.biHeight); bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // calculate size of memory block required to store BITMAPINFO dwLen = bi.biSize + PaletteSize(bi) + bi.biSizeImage; hDIB = System.Runtime.InteropServices.Marshal.AllocHGlobal(dwLen); if (hDIB == IntPtr.Zero) { // uh oh return(null); } unsafe { // lock memory block and get pointer to it BITMAPINFOHEADER *lpbi = (BITMAPINFOHEADER *)GlobalLock(hDIB); // Daten in den Puffer kopieren *lpbi = bi; // Bild-Daten kopieren switch (bi.biBitCount) { case 1: { // Palette in DC setzen if (PaletteEntryCount > 0) { RGBQUAD *bmiColor; bmiColor = (RGBQUAD *)((byte *)lpbi + lpbi->biSize); for (int i = 0; i < 2; i++) { bmiColor[i].rgbRed = PaletteRed(i); bmiColor[i].rgbGreen = PaletteGreen(i); bmiColor[i].rgbBlue = PaletteBlue(i); bmiColor[i].rgbReserved = 0; } } byte *pData = (byte *)lpbi + lpbi->biSize + PaletteSize(bi); int iLO = Width / 8; if ((Width & 7) != 0) { iLO++; } if ((iLO % 4) != 0) { iLO += 4 - (iLO % 4); } /* * GR::Graphic::ContextDescriptor cdImage( Image ); * GR::Graphic::ContextDescriptor cdTarget; * * cdTarget.Attach( cdImage.Width(), cdImage.Height(), iLO, cdImage.ImageFormat(), pData ); * * for ( int j = 0; j < Image.Height(); j++ ) * { * cdTarget.HLine( 0, cdTarget.Width() - 1, j, 1 ); * cdTarget.HLine( 1, cdTarget.Width() - 2, j, 0 ); * }*/ } break; case 4: { // Palette in DC setzen if (PaletteEntryCount > 0) { RGBQUAD *bmiColor = (RGBQUAD *)((byte *)lpbi + lpbi->biSize); for (int i = 0; i < 16; i++) { bmiColor[i].rgbRed = PaletteRed(i); bmiColor[i].rgbGreen = PaletteGreen(i); bmiColor[i].rgbBlue = PaletteBlue(i); bmiColor[i].rgbReserved = 0; } } byte *pData = (byte *)lpbi + lpbi->biSize + PaletteSize(bi); int iLO = Width / 2; if ((Width & 1) != 0) { iLO++; } if ((iLO % 4) != 0) { iLO += 4 - (iLO % 4); } for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { ((byte *)pData)[i + (Height - j - 1) * iLO] = (byte)GetPixel(i, j); } } /* * GR::Graphic::ContextDescriptor cdImage( Image ); * GR::Graphic::ContextDescriptor cdTarget; * * cdTarget.Attach( cdImage.Width(), cdImage.Height(), iLO, cdImage.ImageFormat(), pData ); * * for ( int j = 0; j < Image.Height(); j++ ) * { * cdImage.CopyLine( 0, j, cdImage.Width(), 0, cdImage.Height() - j - 1, &cdTarget ); * }*/ } break; case 8: { // Palette in DC setzen if (PaletteEntryCount > 0) { RGBQUAD *bmiColor; bmiColor = (RGBQUAD *)((byte *)lpbi + lpbi->biSize); for (int i = 0; i < 256; i++) { bmiColor[i].rgbRed = PaletteRed(i); bmiColor[i].rgbGreen = PaletteGreen(i); bmiColor[i].rgbBlue = PaletteBlue(i); bmiColor[i].rgbReserved = 0; } } byte *pData = (byte *)lpbi + lpbi->biSize + PaletteSize(bi); int iLO = Width; if ((iLO % 4) != 0) { iLO += 4 - (iLO % 4); } for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { ((byte *)pData)[i + (Height - j - 1) * iLO] = (byte)GetPixel(i, j); } } } break; case 16: { byte *pData = (byte *)lpbi + lpbi->biSize + PaletteSize(bi); int iLO = Width * 2; if ((iLO % 4) != 0) { iLO += 4 - (iLO % 4); } for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { ((ushort *)pData)[i + (Height - j - 1) * iLO / 2] = (ushort)GetPixel(i, j); } } } break; /* * case 24: * { * byte *pData = (byte*)lpbi + lpbi->biSize + PaletteSize( bi ); * * int iLO = Width * 3; * if ( ( iLO % 4 ) != 0 ) * { * iLO += 4 - ( iLO % 4 ); * } * for ( int j = 0; j < Height; j++ ) * { * for ( int i = 0; i < Width; i++ ) * { * ( (byte*)pData )[3 * i + ( Height - j - 1 ) * iLO] = (byte)*( (byte*)Image.Data() + 3 * ( i + j * Image.Width() ) ); * ( (byte*)pData )[3 * i + ( Height - j - 1 ) * iLO + 1] = (byte)*( (byte*)Image.Data() + 3 * ( i + j * Image.Width() ) + 1 ); * ( (byte*)pData )[3 * i + ( Height - j - 1 ) * iLO + 2] = (byte)*( (byte*)Image.Data() + 3 * ( i + j * Image.Width() ) + 2 ); * } * } * } * break;*/ case 32: { byte *pData = (byte *)lpbi + lpbi->biSize + PaletteSize(bi); int iLO = Width; for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { ((uint *)pData)[i + (Height - j - 1) * iLO] = GetPixel(i, j); } } } break; default: Debug.Log("CreateHDIBAsBuffer unsupported depth " + bi.biBitCount); break; } byte *pDIBData = (byte *)lpbi; for (int i = 0; i < bi.biSize + PaletteSize(bi) + bi.biSizeImage; ++i) { result.AppendU8(pDIBData[i]); } //bi = *lpbi; GlobalUnlock(hDIB); System.Runtime.InteropServices.Marshal.FreeHGlobal(hDIB); } return(result); }