public override void UpdateNormalBitmap(System.Windows.Media.Imaging.WriteableBitmap bitmap, System.Windows.Media.Color color) { unsafe { bitmap.Lock(); int currentPixel = -1; byte * pStart = (byte *)(void *)bitmap.BackBuffer; double iRowUnit = (double)(MaxValue - MinValue) / bitmap.PixelHeight; double iRowCurrent = 100; double l = sModel.LComponent(color); double a = sModel.AComponent(color); for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) { Color lightness = sModel.Color(l, a, iRowCurrent); for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) { currentPixel++; *(pStart + currentPixel * 3 + 0) = lightness.B; //Blue *(pStart + currentPixel * 3 + 1) = lightness.G; //Green *(pStart + currentPixel * 3 + 2) = lightness.R; //red } iRowCurrent -= iRowUnit; } bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); } }
internal static bool CopyGdipBitmapToWicBitmap(System.Drawing.Bitmap gdipBitmap, System.Windows.Media.Imaging.WriteableBitmap wicBitmap) { System.Diagnostics.Debug.Assert(gdipBitmap != null && wicBitmap != null); if (wicBitmap.Format != System.Windows.Media.PixelFormats.Pbgra32) { return(false); } if (gdipBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) { return(false); } wicBitmap.Lock(); // GDI+ ビットマップから WIC ビットマップへ転送する際、ウィンドウ矩形やユーザー定義クリッピング矩形を考慮すれば、もっと効率化できる? // 単純にそのままごっそりブロック コピーしてしまったほうがむしろ速い? var gdipBitmapLockData = gdipBitmap.LockBits( new System.Drawing.Rectangle(0, 0, gdipBitmap.Width, gdipBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, gdipBitmap.PixelFormat); // GDI+ の PixelFormat と WIC の PixelFormat はメンバーの命名規則が異なるが、 // どちらも DIB は GDI からの仕様に準じているため、BGRA の順で並んでいる。 // GDI+ の命名は昔の Direct3D の D3DFMT_A8R8G8B8 に、WIC の命名は DXGI の DXGI_FORMAT_B8G8R8A8_UNORM に近い。 // ともに 32bit であればパディングも考慮する必要がないので、そのまま高速にブロック コピーできる。 Kernel32DllMethodsInvoker.CopyMemory(wicBitmap.BackBuffer, gdipBitmapLockData.Scan0, new IntPtr(wicBitmap.PixelHeight * wicBitmap.BackBufferStride)); gdipBitmap.UnlockBits(gdipBitmapLockData); wicBitmap.AddDirtyRect(new System.Windows.Int32Rect(0, 0, wicBitmap.PixelWidth, wicBitmap.PixelHeight)); wicBitmap.Unlock(); return(true); }
public override void UpdateColorPlaneBitmap(System.Windows.Media.Imaging.WriteableBitmap bitmap, int normalComponentValue) { unsafe { bitmap.Lock(); byte * pStart = (byte *)(void *)bitmap.BackBuffer; int currentPixel = -1; double iRowUnit = (double)100 / bitmap.PixelHeight; double iColUnit = (double)1; double iRowCurrent = 100; double b = (double)normalComponentValue; for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) { double l = iRowCurrent; double iColCurrent = -128; for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) { double theta = 6.0 / 29.0; double a = iColCurrent; double fy = (l + 16) / 116.0; double fx = fy + (a / 500.0); double fz = fy - (b / 200.0); var x = (fx > theta) ? D65X * (fx * fx * fx) : (fx - 16.0 / 116.0) * 3 * (theta * theta) * D65X; var y = (fy > theta) ? D65Y * (fy * fy * fy) : (fy - 16.0 / 116.0) * 3 * (theta * theta) * D65Y; var z = (fz > theta) ? D65Z * (fz * fz * fz) : (fz - 16.0 / 116.0) * 3 * (theta * theta) * D65Z; x = (x > 0.9505) ? 0.9505 : ((x < 0) ? 0 : x); y = (y > 1.0) ? 1.0 : ((y < 0) ? 0 : y); z = (z > 1.089) ? 1.089 : ((z < 0) ? 0 : z); double[] Clinear = new double[3]; Clinear[0] = x * 3.2410 - y * 1.5374 - z * 0.4986; // red Clinear[1] = -x * 0.9692 + y * 1.8760 - z * 0.0416; // green Clinear[2] = x * 0.0556 - y * 0.2040 + z * 1.0570; // blue for (int i = 0; i < 3; i++) { Clinear[i] = (Clinear[i] <= 0.0031308) ? 12.92 * Clinear[i] : (1 + 0.055) * Math.Pow(Clinear[i], (1.0 / 2.4)) - 0.055; Clinear[i] = Math.Min(Clinear[i], 1); Clinear[i] = Math.Max(Clinear[i], 0); } currentPixel++; *(pStart + currentPixel * 3 + 0) = Convert.ToByte(Clinear[2] * 255); //Blue *(pStart + currentPixel * 3 + 1) = Convert.ToByte(Clinear[1] * 255); //Green *(pStart + currentPixel * 3 + 2) = Convert.ToByte(Clinear[0] * 255); //red iColCurrent += iColUnit; } iRowCurrent -= iRowUnit; } bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); } }
public WPFMemoryManager(System.Windows.Media.Imaging.WriteableBitmap bmp) { bmp.Lock(); if (!_Implementation.TryGetExactPixelFormat(bmp.Format, out var bmpFmt)) { throw new Diagnostics.PixelFormatNotSupportedException(bmp.Format, nameof(bmp)); } var nfo = new BitmapInfo(bmp.PixelWidth, bmp.PixelHeight, bmpFmt, bmp.BackBufferStride); var ptr = new PointerBitmap(bmp.BackBuffer, nfo); }
public static void SetPixels(WIC_WRITABLE dstBmp, int dstX, int dstY, PointerBitmap srcPtr) { if (dstX < 0) { throw new ArgumentOutOfRangeException(nameof(dstX)); } if (dstY < 0) { throw new ArgumentOutOfRangeException(nameof(dstY)); } if (dstX + srcPtr.Width > dstBmp.PixelWidth) { throw new ArgumentOutOfRangeException(nameof(srcPtr.Width)); } if (dstY + srcPtr.Height > dstBmp.PixelHeight) { throw new ArgumentOutOfRangeException(nameof(srcPtr.Height)); } if (!TryGetExactPixelFormat(dstBmp.Format, out var dstFmt)) { throw new Diagnostics.PixelFormatNotSupportedException(dstBmp.Format, nameof(dstBmp)); } if (srcPtr.PixelFormat == dstFmt) { var rect = new System.Windows.Int32Rect(dstX, dstY, dstBmp.PixelWidth, dstBmp.PixelHeight); dstBmp.WritePixels(rect, srcPtr.Pointer, srcPtr.Info.BitmapByteSize, srcPtr.Info.StepByteSize); return; } try { dstBmp.Lock(); var nfo = new BitmapInfo(dstBmp.PixelWidth, dstBmp.PixelHeight, dstFmt, dstBmp.BackBufferStride); var dstPtr = new PointerBitmap(dstBmp.BackBuffer, nfo); dstPtr.AsSpanBitmap().SetPixels(0, 0, srcPtr.AsSpanBitmap()); var w = Math.Min(dstBmp.PixelWidth, srcPtr.Width); var h = Math.Min(dstBmp.PixelHeight, srcPtr.Height); var drect = new System.Windows.Int32Rect(0, 0, w, h); dstBmp.AddDirtyRect(drect); } finally { dstBmp.Unlock(); } }
private static void SaveImageAsPngFile(System.Windows.Media.Imaging.WriteableBitmap bitmap, string fileName, IntPtr hwnd, System.Windows.Int32Rect clippingRect) { if (bitmap == null) { throw new ArgumentNullException("Invalid bitmap!!"); } if (!User32DllMethodsInvoker.IsWindow(hwnd)) { throw new ArgumentException("Invalid window!!"); } // クライアント矩形やユーザー定義クリッピング矩形で切り出した内容のみを保存する。 var intersectRect = MyWin32InteropHelper.CalcClientIntersectRect(hwnd, clippingRect); System.Diagnostics.Debug.Assert(intersectRect.HasArea); var tempSubBitmap = new System.Windows.Media.Imaging.WriteableBitmap(intersectRect.Width, intersectRect.Height, MyDeviceHelper.DefaultDpi, MyDeviceHelper.DefaultDpi, System.Windows.Media.PixelFormats.Pbgra32, null); try { tempSubBitmap.Lock(); bitmap.CopyPixels(new System.Windows.Int32Rect(0, 0, intersectRect.Width, intersectRect.Height), tempSubBitmap.BackBuffer, tempSubBitmap.PixelWidth * tempSubBitmap.PixelHeight * 4, tempSubBitmap.PixelWidth * 4); } catch (Exception) { throw; } finally { tempSubBitmap.Unlock(); } using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); //encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmap)); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(tempSubBitmap)); encoder.Save(stream); } tempSubBitmap = null; }
public BitmapData Lock() { var wb = Control as swm.Imaging.WriteableBitmap; if (wb != null) { wb.Lock(); return(new BitmapDataHandler(wb.BackBuffer, (int)stride, Control)); } else { wb = new swm.Imaging.WriteableBitmap(Control); wb.Lock(); Control = wb; return(new BitmapDataHandler(wb.BackBuffer, (int)stride, wb)); } }
public override unsafe System.Windows.Media.Imaging.BitmapSource ToBitmapSource() { System.Windows.Media.Imaging.WriteableBitmap bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Cmyk32, null ); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { ColorCmyk8 *src = (ColorCmyk8 *)GetRowAddressUnchecked(y); byte * dst = destScan0 + (y * destStride); for (int x = 0; x < width; x++) { dst[0] = src->C; dst[1] = src->M; dst[2] = src->Y; dst[3] = src->K; src++; dst += 4; } } } finally { bitmap.Unlock(); } bitmap.Freeze(); return(bitmap); }
public override unsafe System.Windows.Media.Imaging.BitmapSource ToBitmapSource() { System.Windows.Media.Imaging.WriteableBitmap bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Gray8, null ); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { byte *src = GetRowAddressUnchecked(y); byte *dst = destScan0 + (y * destStride); for (int x = 0; x < width; x++) { *dst = *src; src++; dst++; } } } finally { bitmap.Unlock(); } bitmap.Freeze(); return(bitmap); }
public override unsafe System.Windows.Media.Imaging.BitmapSource ToBitmapSource() { System.Windows.Media.Imaging.WriteableBitmap bitmap; if (HasTransparency()) { bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Rgba64, null); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { ColorBgra16 *src = (ColorBgra16 *)GetRowAddressUnchecked(y); ushort * dst = (ushort *)(destScan0 + (y * destStride)); for (int x = 0; x < width; x++) { dst[0] = Fix16BitRange(src->R); dst[1] = Fix16BitRange(src->G); dst[2] = Fix16BitRange(src->B); dst[3] = Fix16BitRange(src->A); src++; dst += 4; } } } finally { bitmap.Unlock(); } } else { bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Rgb48, null); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { ColorBgra16 *src = (ColorBgra16 *)GetRowAddressUnchecked(y); ushort * dst = (ushort *)(destScan0 + (y * destStride)); for (int x = 0; x < width; x++) { dst[0] = Fix16BitRange(src->R); dst[1] = Fix16BitRange(src->G); dst[2] = Fix16BitRange(src->B); src++; dst += 3; } } } finally { bitmap.Unlock(); } } bitmap.Freeze(); return(bitmap); }
/// <summary> /// Convert HObject to BitmapImage /// </summary> /// <param name="HImg"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage Hobject2Bitmapimage(HalconDotNet.HObject HImg) { if (HImg == null) { return(null); } try { System.Windows.Media.Imaging.BitmapImage bmpimg = new System.Windows.Media.Imaging.BitmapImage(); HalconDotNet.HTuple Channels; HalconDotNet.HOperatorSet.CountChannels(HImg, out Channels); if (Channels.I == 1) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Gray8; HalconDotNet.HTuple hpointer, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer1(HImg, out hpointer, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *img_ptr = (byte *)hpointer.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + i) = (*(img_ptr + i)); } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else if (Channels.I == 3) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Bgr24; HalconDotNet.HTuple hred, hgreen, hblue, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *hr = (byte *)hred.IP; byte *hg = (byte *)hgreen.IP; byte *hb = (byte *)hblue.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + (i * 3)) = (*(hb + i)); *(data + (i * 3) + 1) = *(hg + i); *(data + (i * 3) + 2) = *(hr + i); } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else if (Channels.I == 4) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Pbgra32; HalconDotNet.HTuple hred, hgreen, hblue, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *hr = (byte *)hred.IP; byte *hg = (byte *)hgreen.IP; byte *hb = (byte *)hblue.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + (i * 4)) = *(hb + i); *(data + (i * 4) + 1) = *(hg + i); *(data + (i * 4) + 2) = *(hr + i); *(data + (i * 4) + 3) = 255; } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else { bmpimg = null; } return(bmpimg); } catch (HalconDotNet.HalconException ex) { Console.WriteLine("In ImageTypeConverter.Hobject2Bitmapimage: " + ex.Message); return(null); } catch (System.Exception ex) { Console.WriteLine("In ImageTypeConverter.Hobject2Bitmapimage: " + ex.Message); return(null); } }