public override BitmapSource ExecuteFilter(BitmapSource inputImage) { byte[] sourceBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; byte[] destBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; inputImage.CopyPixels(sourceBytes, inputImage.PixelWidth * 4, 0); float redAdjust = 1.0f + (this.Warmth / 100.0f); float blueAdjust = 1.0f - (this.Warmth / 100.0f); GaussianBlurFilter blur = new GaussianBlurFilter(3 * Softness); BrightnessContrastFilter bc = new BrightnessContrastFilter(Lighting, -Lighting / 2); DesaturateFilter desat = new DesaturateFilter(); inputImage = blur.ExecuteFilter(inputImage); inputImage.CopyPixels(destBytes, inputImage.PixelWidth * 4, 0); int width = inputImage.PixelWidth; int height = inputImage.PixelHeight; for (int i = 0; i < sourceBytes.Length; i += 4) { byte b = sourceBytes[i]; byte g = sourceBytes[i + 1]; byte r = sourceBytes[i + 2]; byte a = sourceBytes[i + 3]; bc.PerformOperation(ref b, ref g, ref r, ref a); desat.PerformOperation(ref b, ref g, ref r, ref a); double rD = r * redAdjust; double bD = r * blueAdjust; if (rD > 255) rD = 255; else if (rD < 0) rD = 0; if (bD > 255) bD = 255; else if (bD < 0) bD = 0; destBytes[i] = PixelBlend.BlendOverlay(b, destBytes[i]); destBytes[i + 1] = PixelBlend.BlendOverlay(g, destBytes[i + 1]); destBytes[i + 2] = PixelBlend.BlendOverlay(r, destBytes[i + 2]); destBytes[i + 3] = PixelBlend.BlendOverlay(a, destBytes[i + 3]); } return BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, 96, 96, inputImage.Format, null, destBytes, inputImage.PixelWidth * 4); }
public static SharpDX.Direct2D1.Bitmap Test(SharpDX.Direct2D1.DeviceContext dc, ImagingFactory factory, System.Windows.Media.Imaging.BitmapSource src) { // PixelFormat settings/conversion if (src.Format != System.Windows.Media.PixelFormats.Bgra32) { // Convert BitmapSource FormatConvertedBitmap fcb = new FormatConvertedBitmap(); fcb.BeginInit(); fcb.Source = src; fcb.DestinationFormat = PixelFormats.Bgra32; fcb.EndInit(); src = fcb; } SharpDX.Direct2D1.Bitmap retval = null; try { int stride = src.PixelWidth * (src.Format.BitsPerPixel + 7) / 8; int bufferSize = stride * src.PixelHeight; byte[] buffer = new byte[bufferSize]; src.CopyPixels(Int32Rect.Empty, buffer, stride, 0); GCHandle pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr pointer = pinnedArray.AddrOfPinnedObject(); SharpDX.DataStream datastream = new SharpDX.DataStream(pointer, bufferSize, true, true); var bmpProps1 = new SharpDX.Direct2D1.BitmapProperties1(dc.PixelFormat, dc.Factory.DesktopDpi.Width, dc.Factory.DesktopDpi.Height); retval = new SharpDX.Direct2D1.Bitmap1(dc, new SharpDX.Size2(src.PixelWidth, src.PixelHeight), datastream, stride, bmpProps1); pinnedArray.Free(); } catch (Exception e) { } return(retval); }
public static BitmapSource CopyBitmapSource(BitmapSource source) { // Calculate stride of source int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8); // Create data array to hold source pixel data byte[] data = new byte[stride * source.PixelHeight]; // Copy source image pixels to the data array source.CopyPixels(data, stride, 0); // Create WriteableBitmap to copy the pixel data to. WriteableBitmap target = new WriteableBitmap( source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY, source.Format, null); // Write the pixel data to the WriteableBitmap. target.WritePixels( new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), data, stride, 0); return target; }
public override BitmapSource ExecuteFilter(BitmapSource inputImage) { byte[] srcBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; byte[] finalBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; inputImage.CopyPixels(srcBytes, inputImage.PixelWidth * 4, 0); BrightnessContrastFilter bcFilter = new BrightnessContrastFilter(Brightness, Contrast); GaussianBlurFilter blurFilter = new GaussianBlurFilter(Radius); BitmapSource glow = blurFilter.ExecuteFilter(inputImage); byte[] glowBytes = new byte[glow.PixelWidth * glow.PixelHeight * 4]; glow.CopyPixels(glowBytes, glow.PixelWidth * 4, 0); for (int i = 0; i < glowBytes.Length; i+=4) { byte b = glowBytes[i]; byte g = glowBytes[i + 1]; byte r = glowBytes[i + 2]; byte a = glowBytes[i + 3]; bcFilter.PerformOperation(ref b, ref g, ref r, ref a); finalBytes[i] = PixelBlend.BlendScreen(srcBytes[i], b); finalBytes[i + 1] = PixelBlend.BlendScreen(srcBytes[i + 1], g); finalBytes[i + 2] = PixelBlend.BlendScreen(srcBytes[i + 2], r); finalBytes[i + 3] = 255; } return BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, 96, 96, inputImage.Format, null, finalBytes, inputImage.PixelWidth * 4); }
/// <summary> /// 更改颜色。 /// </summary> /// <param name="pic">原图</param> /// <param name="srcBGRA">要更改的像素,顺序为蓝绿红透明通道</param> /// <param name="dstBGRA">要更改为的像素,顺序为蓝绿红透明通道</param> /// <param name="isIgnoreAlpha">是否忽略Alpha通道的对照值</param> /// <returns>更改后的图片</returns> public BitmapSource ChangeColor(BitmapSource pic, byte[] srcBGRA, byte[] dstBGRA, bool isIgnoreAlpha) { int PixelWidth = pic.PixelWidth; int PixelHeight = pic.PixelHeight; byte[] picArray = new byte[PixelWidth * PixelHeight * 4]; pic.CopyPixels(picArray, PixelWidth * 4, 0); if (picArray[0] == srcBGRA[0]) { picArray[0] = dstBGRA[0]; } if (picArray[1] == srcBGRA[1]) { picArray[1] = dstBGRA[1]; } if (picArray[2] == srcBGRA[2]) { picArray[2] = dstBGRA[2]; } if (picArray[3] == srcBGRA[3]) { picArray[3] = dstBGRA[3]; } for (int i = 4; i < picArray.Count(); i++) { if (i % 4 == 0) { if (picArray[i] == srcBGRA[0]) { picArray[i] = dstBGRA[0]; } } if (i % 4 == 1) { if (picArray[i] == srcBGRA[1]) { picArray[i] = dstBGRA[1]; } } if (i % 4 == 2) { if (picArray[i] == srcBGRA[2]) { picArray[i] = dstBGRA[2]; } } if (i % 4 == 3) { if (isIgnoreAlpha) { picArray[i] = dstBGRA[3]; } else { if (picArray[i] == srcBGRA[3]) { picArray[i] = dstBGRA[3]; } } } } BitmapSource bpic = BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null, picArray, PixelWidth * 4); return bpic; }
private void CalculateLuminanceRGB(BitmapSource bitmap) { var width = bitmap.PixelWidth; var height = bitmap.PixelHeight; var stepX = (bitmap.Format.BitsPerPixel + 7) / 8; var bufferSize = width * stepX; var buffer = new byte[bufferSize]; var rect = new Int32Rect(0, 0, width, 1); var luminanceIndex = 0; luminances = new byte[width * height]; for (var curY = 0; curY < height; curY++) { bitmap.CopyPixels(rect, buffer, bufferSize, 0); for (var curX = 0; curX < bufferSize; curX += stepX) { var r = buffer[curX]; var g = buffer[curX + 1]; var b = buffer[curX + 2]; luminances[luminanceIndex] = (byte) (0.3 * r + 0.59 * g + 0.11 * b + 0.01); luminanceIndex++; } rect.Y++; } }
public GrayscaleImageAdapter(BitmapSource rgbSource) { if (rgbSource == null) { throw new ArgumentNullException("source"); } int width = rgbSource.PixelWidth; int height = rgbSource.PixelHeight; GrayscalePixel[,] result = new GrayscalePixel[height, width]; byte[] byteArray = new byte[height * width * 4]; byte[] grayscaleArray = new byte[height * width]; int stride = width * 4; rgbSource.CopyPixels(byteArray, stride, 0); int k = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { byte blue = byteArray[(i * width + j) * 4 + 0]; byte green = byteArray[(i * width + j) * 4 + 1]; byte red = byteArray[(i * width + j) * 4 + 2]; GrayscalePixel grayscalePixel = new GrayscalePixel(Convert.ToByte(0.2125 * red + 0.7154 * green + 0.0721 * blue)); result[i, j] = grayscalePixel; grayscaleArray[k++] = grayscalePixel.Level; } } this.image = new GrayscaleImage(result); this.source = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Gray8, null, grayscaleArray, image.Width); }
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs) { System.Drawing.Bitmap temp = null; System.Drawing.Bitmap result; System.Drawing.Graphics g; int width = srs.PixelWidth; int height = srs.PixelHeight; int stride = width * ((srs.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; srs.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pB = bits) { IntPtr ptr = new IntPtr(pB); temp = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); } } // Copy the image back into a safe structure result = new System.Drawing.Bitmap(width, height); g = System.Drawing.Graphics.FromImage(result); g.DrawImage(temp, 0, 0); g.Dispose(); return result; }
// get raw bytes from BitmapImage using BitmapImage.CopyPixels private byte[] GetImageByteArray(BitmapSource bi) { var rawStride = (bi.PixelWidth * bi.Format.BitsPerPixel + 7) / 8; var result = new byte[rawStride * bi.PixelHeight]; bi.CopyPixels(result, rawStride, 0); return result; }
public Bitmap ImageCovert(System.Windows.Controls.Image image) { System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = image.Source as BitmapSource; int width = transformedBitmapSource.PixelWidth; int height = transformedBitmapSource.PixelHeight; int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; transformedBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed(byte *pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); return(bitmap); } } }
public ImageBuffer(BitmapSource bitmap) { _height = bitmap.PixelHeight; _width = bitmap.PixelWidth; _stride = _width*4; _buffer = new byte[_height * _stride]; bitmap.CopyPixels(Int32Rect.Empty, _buffer, _stride, 0); }
private Bitmap GetBitmap(BitmapSource source) { var bmp = new Bitmap(source.PixelWidth, source.PixelHeight, PixelFormat.Format32bppPArgb); var data = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb); source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; }
private static byte[] GetArrayOfPixels(BitmapSource bitmapsource) { Int32 stride = bitmapsource.PixelWidth * bitmapsource.Format.BitsPerPixel / 8; Int32 ByteSize = stride * bitmapsource.PixelHeight * bitmapsource.Format.BitsPerPixel / 8; byte[] arrayofpixel = new byte[ByteSize]; bitmapsource.CopyPixels(arrayofpixel, stride, 0); return arrayofpixel; }
public static byte[] GetBinaryFromWpfBmpSource(System.Windows.Media.Imaging.BitmapSource srcBitmap) { int stride = (srcBitmap.PixelWidth * (srcBitmap.Format.BitsPerPixel / 8)); byte[] bin = new byte[srcBitmap.PixelHeight * stride]; srcBitmap.CopyPixels(bin, stride, 0); return(bin); }
private static Bitmap bitmapSourceToBitmap(BitmapSource source) { Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; }
static byte[] ConvertImageDataToByteArray(BitmapSource bitmap) { var stride = bitmap.PixelWidth * ((bitmap.Format.BitsPerPixel + 7) / 8); var imageData = new byte[bitmap.PixelHeight * stride]; bitmap.CopyPixels(imageData, stride, 0); return imageData; }
private System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource) { Bitmap bmp = new Bitmap(bitmapsource.PixelWidth, bitmapsource.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); BitmapData data = bmp.LockBits(new Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bitmapsource.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; }
public static unsafe void CopyPixels(BitmapSource source, PixelColor[,] pixels, int stride, int offset) { fixed (PixelColor* buffer = &pixels[0, 0]) source.CopyPixels( new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), (IntPtr)(buffer + offset), pixels.GetLength(0) * pixels.GetLength(1) * sizeof(PixelColor), stride); }
static Bitmap GetBitmap(BitmapSource bitmapSource) { var format = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; Bitmap bmp = new Bitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, format); BitmapData data = bmp.LockBits(new Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, format); bitmapSource.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; }
public BitmapSource LoadImageFromExtraImage(ExtraImage item, BitmapSource extraImage) { var image = new WriteableBitmap(item.Width, item.Height, 96, 96, PixelFormats.Pbgra32, null); var rect = new Int32Rect(item.X, item.Y, item.Width, item.Height); image.Lock(); extraImage.CopyPixels(rect, image.BackBuffer, image.BackBufferStride * image.PixelHeight, image.BackBufferStride); image.Unlock(); image.Freeze(); return image; }
public static System.Drawing.Bitmap BitmapSourceToBitmap2(BitmapSource srs) { System.Drawing.Bitmap btm = null; int width = srs.PixelWidth; int height = srs.PixelHeight; int stride = width * ((srs.Format.BitsPerPixel + 7) / 8); IntPtr ptr = Marshal.AllocHGlobal(height * stride); srs.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride); btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ptr); return btm; }
public static byte[] GetBytesFromBitmapSource(BitmapSource bmp) { int width = bmp.PixelWidth; int height = bmp.PixelHeight; int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8); var pixels = new byte[height * stride]; bmp.CopyPixels(pixels, stride, 0); return pixels; }
public static Mat ToMat(BitmapSource source) { if (source.Format == PixelFormats.Bgra32) { Mat result = new Mat(); result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4); source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step*result.Rows, result.Step); return result; } else if (source.Format == PixelFormats.Bgr24) { Mat result = new Mat(); result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 3); source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step); return result; } else { throw new Exception(String.Format("Convertion from BitmapSource of format {0} is not supported.", source.Format)); } }
public static BitmapSource ConvertBitmapTo96DPI (BitmapSource bitmapImage) { double dpi = 96; int width = bitmapImage.PixelWidth; int height = bitmapImage.PixelHeight; int stride = width * (bitmapImage.Format.BitsPerPixel + 7) / 8; byte[] pixelData = new byte[stride * height]; bitmapImage.CopyPixels (pixelData, stride, 0); return BitmapSource.Create (width, height, dpi, dpi, bitmapImage.Format, null, pixelData, stride); }
/// <summary> /// 更改颜色,专门用于上行渐变和下行渐变。 /// </summary> /// <param name="pic">原图</param> /// <param name="dstBGRA">要更改为的像素,顺序为蓝绿红透明通道</param> /// <returns></returns> public BitmapSource ChangeColor(BitmapSource pic, byte[] dstBGRA, bool isUp2Down) { int lineCount = 0; int PixelWidth = pic.PixelWidth; int PixelHeight = pic.PixelHeight; byte[] picArray = new byte[PixelWidth * PixelHeight * 4]; pic.CopyPixels(picArray, PixelWidth * 4, 0); picArray[0] = dstBGRA[0]; picArray[1] = dstBGRA[1]; picArray[2] = dstBGRA[2]; picArray[3] = dstBGRA[3]; for (int i = 4; i < picArray.Count(); i++) { if (i % (4 * 20) == 0) { lineCount++; } if (i % 4 == 0) { int temp = dstBGRA[0]; if (isUp2Down) temp += lineCount * 3; else temp -= lineCount * 3; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; picArray[i] = (byte)temp; } if (i % 4 == 1) { int temp = dstBGRA[1]; if (isUp2Down) temp += lineCount * 3; else temp -= lineCount * 3; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; picArray[i] = (byte)temp; } if (i % 4 == 2) { int temp = dstBGRA[2]; if (isUp2Down) temp += lineCount * 3; else temp -= lineCount * 3; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; picArray[i] = (byte)temp; } if (i % 4 == 3) { int temp = dstBGRA[3] - lineCount * 3; if (temp > 255) temp = 255; picArray[i] = (byte)temp; } } BitmapSource bpic = BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null, picArray, PixelWidth * 4); return bpic; }
public static void CopyBitmap(BitmapSource source, WriteableBitmap target, bool dispatcher, int spacing, bool freezeBitmap) { var width = source.PixelWidth; var height = source.PixelHeight; var stride = width * ((source.Format.BitsPerPixel + 7) / 8); var bits = new byte[height * stride]; source.CopyPixels(bits, stride, 0); if (dispatcher) { target.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(delegate { //UI Thread var delta = target.Height - height; var newWidth = width > target.Width ? (int)target.Width : width; var outRect = new Int32Rect((int)((target.Width - newWidth) / 2), (int)(delta >= 0 ? delta : 0) / 2 + spacing, newWidth - (spacing * 2), newWidth - (spacing * 2)); try { target.WritePixels(outRect, bits, stride, 0); if (freezeBitmap) { target.Freeze(); } } catch (Exception e) { Debug.WriteLine(e); Debugger.Break(); } })); } else { var delta = target.Height - height; var newWidth = width > target.Width ? (int)target.Width : width; var outRect = new Int32Rect(spacing, (int)(delta >= 0 ? delta : 0) / 2 + spacing, newWidth - (spacing * 2), newWidth - (spacing * 2)); try { target.WritePixels(outRect, bits, stride, 0); if (freezeBitmap) target.Freeze(); } catch (Exception e) { Debug.WriteLine(e); Debugger.Break(); } } }
/// <summary> /// This method returns the RgbData struct representing the given BitmapSource /// </summary> /// <param name="bitmapSource">the target BitmapSource</param> /// <returns>RgbData representing the given BitmapSource</returns> public static RgbData getRgbData(BitmapSource bitmapSource) { RgbData rgbData = new RgbData(); rgbData.stride = bitmapSource.PixelWidth * ((bitmapSource.Format.BitsPerPixel + 7) / 8); rgbData.dataLength = rgbData.stride * bitmapSource.PixelHeight; rgbData.pixelWidth = bitmapSource.PixelWidth; rgbData.pixelHeight = bitmapSource.PixelHeight; rgbData.dpiX = bitmapSource.DpiX; rgbData.dpiY = bitmapSource.DpiY; rgbData.pixelFormat = bitmapSource.Format; rgbData.rawRgbByteArray = new byte[rgbData.dataLength]; bitmapSource.CopyPixels(rgbData.rawRgbByteArray, rgbData.stride, 0); return rgbData; }
public static byte[,] GetBytes(BitmapSource source) { var stride = (source.PixelWidth * source.Format.BitsPerPixel + 7) / 8; var buffer = new byte[stride * source.PixelHeight]; source.CopyPixels(buffer, stride, 0); var bytes = new byte[source.PixelWidth, source.PixelHeight]; for(int y = 0; y < source.PixelHeight; y++) { for(int x = 0; x < source.PixelWidth; x++) { bytes[x, y] = buffer[(y * stride) + x]; } } return bytes; }
public ImagePreview(Stream imageStream) { InitializeComponent(); BitmapImage imgSource = new BitmapImage(); imgSource.BeginInit(); imgSource.StreamSource = imageStream; imgSource.EndInit(); img.Source = imgSource; originalSource = (BitmapSource)img.Source; int stride = originalSource.PixelWidth * originalSource.Format.BitsPerPixel / 8; stride = stride + (stride % 4) * 4; originalPixels = new byte[stride * originalSource.PixelHeight * originalSource.Format.BitsPerPixel / 8]; originalSource.CopyPixels(originalPixels, stride, 0); }
public PixelColor[,] GetPixels(BitmapSource source) { var height = source.PixelHeight; var width = source.PixelWidth; var pixelBytes = new byte[height * width * 4]; var pixels = new PixelColor[height, width]; source.CopyPixels(pixelBytes, ((source as BitmapImage).PixelWidth * (source as BitmapImage).Format.BitsPerPixel + 7) / 8, 0); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) pixels[y, x] = new PixelColor { Blue = pixelBytes[(y * width + x) * 4 + 0], Green = pixelBytes[(y * width + x) * 4 + 1], Red = pixelBytes[(y * width + x) * 4 + 2], Alpha = pixelBytes[(y * width + x) * 4 + 3], }; return pixels; }
public Bitmap BitmapSourceToBitmap(BitmapSource imageSource) { System.Drawing.Bitmap bitmap = null; int width = imageSource.PixelWidth; int height = imageSource.PixelHeight; int stride = width * ((imageSource.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; imageSource.CopyPixels(bits, stride, 0); unsafe { fixed (byte* bitPointer = bits) { IntPtr intPointer = new IntPtr(bitPointer); bitmap = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format16bppRgb565, intPointer); } } return bitmap; }
public override BitmapSource ExecuteFilter(BitmapSource source) { //First calculate average deviation of the source image int range = BrightnessHistogram.Max - BrightnessHistogram.Min; double factor = 256.0 / (double)range; byte[] bytes = new byte[source.PixelWidth * source.PixelHeight * 4]; source.CopyPixels(bytes, source.PixelWidth * 4, 0); double mean = BrightnessHistogram.Mean; byte r, g, b; double h, s, l; for (int i = 0; i < bytes.Length; i += 4) { r = bytes[i + 2]; g = bytes[i + 1]; b = bytes[i]; Utility.RGB2HSL(r, g, b, out h, out s, out l); double deviation = l - mean / 256.0; double lprime = .5 + deviation * factor; if (lprime < 0) lprime = 0.0; else if (lprime > 1) lprime = 1.0; Color c = Utility.HSL2RGB(h, s, lprime); bytes[i + 2] = c.R; bytes[i + 1] = c.G; bytes[i + 0] = c.B; } BitmapSource result = BitmapSource.Create(source.PixelWidth, source.PixelHeight, 96, 96, source.Format, null, bytes, source.PixelWidth * 4); return result; }
public static System.Drawing.Bitmap BitmapSourceToBitmap(System.Windows.Media.Imaging.BitmapSource srs) { System.Drawing.Bitmap btm = null; int width = srs.PixelWidth; int height = srs.PixelHeight; int stride = width * ((srs.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; srs.CopyPixels(bits, stride, 0); unsafe { fixed(byte *pB = bits) { IntPtr ptr = new IntPtr(pB); btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); } } return(btm); }
public static Texture2D FromBitmap(Device device, BitmapSource bitmapSource) { // Allocate DataStream to receive the WIC image pixels int stride = bitmapSource.PixelWidth * 4; using (var buffer = new DataStream(bitmapSource.PixelHeight * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource.CopyPixels(new Int32Rect(0, 0, bitmapSource.PixelWidth, bitmapSource.PixelHeight), buffer.DataPointer, (int)buffer.Length, stride); return new Texture2D(device, new Texture2DDescription() { Width = bitmapSource.PixelWidth, Height = bitmapSource.PixelHeight, ArraySize = 1, BindFlags = BindFlags.ShaderResource, Usage = ResourceUsage.Immutable, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }, new DataRectangle(buffer.DataPointer, stride)); } }
//public override event EventHandler<ExceptionEventArgs> DecodeFailed; //public override event EventHandler DownloadCompleted; //public override event EventHandler<ExceptionEventArgs> DownloadFailed; //public override event EventHandler<DownloadProgressEventArgs> DownloadProgress; #region statics /// <summary> /// Converts BitmapSource to Bitmap. /// </summary> /// <param name="sourceWpf">BitmapSource</param> /// <returns>Bitmap</returns> private static System.Drawing.Bitmap ConvertToBitmap(System.Windows.Media.Imaging.BitmapSource sourceWpf) { System.Windows.Media.Imaging.BitmapSource bmpWpf = sourceWpf; // PixelFormat settings/conversion System.Drawing.Imaging.PixelFormat formatBmp = System.Drawing.Imaging.PixelFormat.Format32bppArgb; if (sourceWpf.Format == PixelFormats.Bgr24) { formatBmp = System.Drawing.Imaging.PixelFormat.Format24bppRgb; } else if (sourceWpf.Format == System.Windows.Media.PixelFormats.Pbgra32) { formatBmp = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; } else if (sourceWpf.Format != System.Windows.Media.PixelFormats.Bgra32) { // Convert BitmapSource FormatConvertedBitmap convertWpf = new FormatConvertedBitmap(); convertWpf.BeginInit(); convertWpf.Source = sourceWpf; convertWpf.DestinationFormat = PixelFormats.Bgra32; convertWpf.EndInit(); bmpWpf = convertWpf; } // Copy/Convert to Bitmap var bmp = new System.Drawing.Bitmap(bmpWpf.PixelWidth, bmpWpf.PixelHeight, formatBmp); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size); BitmapData data = bmp.LockBits(rect, ImageLockMode.WriteOnly, formatBmp); bmpWpf.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return(bmp); }