public static Bitmap IBitmapImageToBitmap(IBitmapImage imageBitmap) { if (imageBitmap == null) throw new ArgumentNullException(); Size size; imageBitmap.GetSize(out size); Rectangle rect = new Rectangle(0, 0, size.Width, size.Height); BitmapData lockedBitmapData = new BitmapData(); imageBitmap.LockBits(ref rect, 0U, PixelFormatID.PixelFormat16bppRGB565, out lockedBitmapData); Bitmap bitmap = new Bitmap((int)lockedBitmapData.Width, (int)lockedBitmapData.Height, PixelFormat.Format16bppRgb565); System.Drawing.Imaging.BitmapData bitmapdata = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565); ImageHelpers.CopyMemory(bitmapdata.Scan0, lockedBitmapData.Scan0, (int)(lockedBitmapData.Height * lockedBitmapData.Stride)); imageBitmap.UnlockBits(ref lockedBitmapData); bitmap.UnlockBits(bitmapdata); return bitmap; }
static public Bitmap IBitmapImageToBitmap(IBitmapImage imageBitmap) { if (imageBitmap == null) { throw new ArgumentNullException(); } Size szRotated; imageBitmap.GetSize(out szRotated); RECT rcLock = new RECT(0, 0, szRotated.Width, szRotated.Height); BitmapDataInternal bdi = new BitmapDataInternal(); imageBitmap.LockBits(rcLock, 0, PixelFormat.Format24bppRgb, ref bdi); Bitmap bitmap = new Bitmap(bdi.Width, bdi.Height, bdi.PixelFormat); System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bdi.PixelFormat); CopyMemory(bd.Scan0, bdi.Scan0, bdi.Height * bdi.Stride); imageBitmap.UnlockBits(ref bdi); bitmap.UnlockBits(bd); return(bitmap); }
static public IBitmapImage BitmapToIImageBitmap(Bitmap bitmap) { if (bitmap == null) { throw new ArgumentNullException(); } ImagingFactory factory = new ImagingFactoryClass(); System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); IBitmapImage imageBitmap = null; factory.CreateNewBitmap((uint)bd.Width, (uint)bd.Height, bd.PixelFormat, out imageBitmap); BitmapDataInternal bdi = new BitmapDataInternal(); RECT rc = new RECT(0, 0, bd.Width, bd.Height); imageBitmap.LockBits(rc, (int)ImageLockMode.WriteOnly, bd.PixelFormat, ref bdi); int cb = bdi.Stride * bdi.Height; CopyMemory(bdi.Scan0, bd.Scan0, cb); bitmap.UnlockBits(bd); imageBitmap.UnlockBits(ref bdi); return(imageBitmap); }