unsafe bool Close() { CloseGroup(); if (image != null) { Control.Close(); var handler = (BitmapHandler)image.Handler; var bmp = image.ToWpf(); var newbmp = new swmi.RenderTargetBitmap(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, swm.PixelFormats.Pbgra32); newbmp.RenderWithCollect(visual); if (!bmp.Format.HasAlpha()) { // convert to non-alpha, as RenderTargetBitmap does not support anything other than Pbgra32 var wb = new swmi.WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, swm.PixelFormats.Bgr32, null); var rect = new sw.Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight); var pixels = new byte[bmp.PixelHeight * wb.BackBufferStride]; newbmp.CopyPixels(rect, pixels, wb.BackBufferStride, 0); fixed(byte *ptr = pixels) { wb.WritePixels(rect, (IntPtr)ptr, pixels.Length, wb.BackBufferStride, 0, 0); } handler.SetBitmap(wb); } else { handler.SetBitmap(newbmp); } return(true); } return(false); }
public Color GetAverageColor(Visual visual, Rect area) { var bitmap = new RenderTargetBitmap(1, 1, 96, 96, PixelFormats.Pbgra32); bitmap.Render( new Rectangle { Width = 1, Height = 1, Fill = new VisualBrush { Visual = visual, Viewbox = area } }); var bytes = new byte[4]; bitmap.CopyPixels(bytes, 1, 0); return Color.FromArgb(bytes[0], bytes[1], bytes[2], bytes[3]); }
private void AddFrame(AnimatedGifEncoder encoder) { var rtb = new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(this); var bitmap = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32, null); bitmap.FillRectangle(0, 0, bitmap.PixelWidth, bitmap.PixelWidth, Colors.Wheat); bitmap.Lock(); rtb.CopyPixels( new Int32Rect(0, 0, rtb.PixelWidth, rtb.PixelHeight), bitmap.BackBuffer, bitmap.BackBufferStride * bitmap.PixelHeight, bitmap.BackBufferStride); bitmap.AddDirtyRect(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight)); bitmap.Unlock(); encoder.AddFrame(bitmap); }
public override void Paint(PaintEventArgs args) { if (this.m_Sheet == null) return; RenderTargetBitmap botmap = new RenderTargetBitmap(this.m_Sheet.Width, this.m_Sheet.Height, 96d, 96d, PixelFormats.Default); if (this.m_Sheet.DocumentPage == null && this.m_Sheet.DocumentPageWrapper != null) this.m_Sheet.DocumentPage = this.m_Sheet.DocumentPageWrapper.XPSDocumentPage; if (this.m_Sheet.DocumentPage == null) return; botmap.Render(this.m_Sheet.DocumentPage.Visual); byte[] rgbValues = new byte[this.m_Sheet.Height * this.m_Sheet.Width * 4]; botmap.CopyPixels(rgbValues, this.m_Sheet.Width * 4, 0); using (Bitmap bmp = new Bitmap(this.m_Sheet.Width, this.m_Sheet.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)) { System.Drawing.Imaging.BitmapData bd = bmp.LockBits(new Rectangle(0, 0, this.m_Sheet.Width, this.m_Sheet.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bd.Scan0, rgbValues.Length); bmp.UnlockBits(bd); using (Synchronizer.Lock(this.SlideDisplay.SyncRoot)) { Graphics g = args.Graphics; ///Lock the graphics object so it doesn't get used elsewhere. using (Synchronizer.Lock(g)) { g.Transform = this.SlideDisplay.PixelTransform; using (Synchronizer.Lock(this.m_Sheet.SyncRoot)) { using (Synchronizer.Lock(bmp)) { g.DrawImage(bmp, this.m_Sheet.Bounds.X - 3, this.m_Sheet.Bounds.Y - 3, this.m_Sheet.Bounds.Width, this.m_Sheet.Bounds.Height); } } } } } System.GC.Collect(); }
private static void ThreadFunc(object state) { while (true) { var request = imageQueue.Take(); int width = request.Width; int height = request.Heigth; Image image = new Image { Width = width, Height = height, Source = request.DrawingImage, Stretch = Stretch.Fill }; image.Measure(new Size(width, height)); image.Arrange(new Rect(0, 0, width, height)); image.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.Background); #if false Window window = new Window(); window.Content = image; window.Show(); #endif RenderTargetBitmap renderBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); renderBitmap.Render(image); image.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.Background); int[] pixels = new int[width * height]; renderBitmap.CopyPixels(pixels, (width * PixelFormats.Pbgra32.BitsPerPixel + 7) / 8, 0); resultQueue.Add(pixels); } }
private void saveImageFrame() { // SHOW A MESSAGE TO THE USER // For memory testing when not using kinect WriteableBitmap currentFrameCopy; if (this.colorBitmap == null) { currentFrameCopy = new WriteableBitmap((int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight, 96, 96, PixelFormats.Pbgra32, null); currentFrameCopy.Lock(); RenderTargetBitmap rendrBMP = new RenderTargetBitmap((int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight, 96, 96, PixelFormats.Pbgra32); rendrBMP.Render(kiddieHolder); rendrBMP.CopyPixels(new Int32Rect(0, 0, (int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight), currentFrameCopy.BackBuffer, currentFrameCopy.BackBufferStride * currentFrameCopy.PixelHeight, currentFrameCopy.BackBufferStride); currentFrameCopy.Unlock(); } else { currentFrameCopy = this.colorBitmap.Clone(); } currentFrameCopy.Freeze(); _photoSubmitter.encodeAndSubmitUserPhotoToCMS(currentFrameCopy); }
private ImageSource RenderBitmapAndCapturePixels(MediaPlayer mediaPlayer, uint[] pixels) { // Render the current frame into a bitmap var drawingVisual = new DrawingVisual(); var renderTargetBitmap = new RenderTargetBitmap(mediaPlayer.NaturalVideoWidth, mediaPlayer.NaturalVideoHeight, 96, 96, PixelFormats.Default); using (var drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawVideo(mediaPlayer, new Rect(0, 0, mediaPlayer.NaturalVideoWidth, mediaPlayer.NaturalVideoHeight)); } renderTargetBitmap.Render(drawingVisual); // Copy the pixels to the specified location renderTargetBitmap.CopyPixels(pixels, mediaPlayer.NaturalVideoWidth * 4, 0); // Return the bitmap return renderTargetBitmap; }
/// <summary>壁重み用の画像を作成する</summary> private void UpdateWallWeight() { // 窓がある部分の周囲をぼかして塗りつぶした画像を作成 DrawingVisual blurVisual = new DrawingVisual(); blurVisual.Effect = _blurEffect; using (DrawingContext dc = blurVisual.RenderOpen()) { // 一旦塗りつぶし dc.DrawRectangle(_aroundWinFGBrush, null, new Rect(-_screenRect.Width / 2.0, -_screenRect.Height / 2.0, _screenRect.Width * 2, _screenRect.Height * 2)); // スクリーンの境界部分のぼかしを作る dc.DrawRectangle(_aroundWinBGBrush, null, new Rect(_blurEffect.Radius / 2.0, _blurEffect.Radius / 2.0, _screenRect.Width - _blurEffect.Radius, _screenRect.Height - _blurEffect.Radius)); // 各窓の領域を塗りつぶし foreach (WindowInfo info in _wkgWindows) { if (info.IsMaximized) break; if (info.IsMinimized) continue; dc.DrawRectangle(_aroundWinFGBrush, null, new Rect(info.Rect.X - _screenRect.X - _blurEffect.Radius / 2.0, info.Rect.Y - _screenRect.Y - _blurEffect.Radius / 2.0, info.Rect.Width + _blurEffect.Radius, info.Rect.Height + _blurEffect.Radius)); } } blurVisual.Clip = new RectangleGeometry(new Rect(0, 0, _screenRect.Width, _screenRect.Height)); // 窓内部を塗りつぶした画像を作成 DrawingVisual noblurVisual = new DrawingVisual(); using (DrawingContext dc = noblurVisual.RenderOpen()) { dc.DrawRectangle(_innerWinBGBrush, null, new Rect(0, 0, _screenRect.Width, _screenRect.Height)); foreach (WindowInfo info in _wkgWindows) { if (info.IsMaximized) break; if (info.IsMinimized) continue; dc.DrawRectangle(_innerWinFGBrush, null, new Rect(info.Rect.X, info.Rect.Y, info.Rect.Width, info.Rect.Height)); } } noblurVisual.Clip = new RectangleGeometry(new Rect(0, 0, _screenRect.Width, _screenRect.Height)); // 2枚の画像を重畳 DrawingVisual _wkgDrawingVisual = new DrawingVisual(); VisualBrush vb1 = new VisualBrush(blurVisual); VisualBrush vb2 = new VisualBrush(noblurVisual); Effect.ColorMergeEffect ef = new Effect.ColorMergeEffect(); ef.Key = (double)Effect.ColorMergeEffect.ColorKey.Green; ef.Input2 = vb2; _wkgDrawingVisual.Effect = ef; using (DrawingContext dc = _wkgDrawingVisual.RenderOpen()) { dc.DrawRectangle(vb1, null, new Rect(0, 0, _screenRect.Width, _screenRect.Height)); } // 画像から壁重みの配列に変換 _rBmp = new RenderTargetBitmap((int)_screenRect.Width, (int)_screenRect.Height, Common.Constants.DPIX, Common.Constants.DPIY, PixelFormats.Pbgra32); _rBmp.Render(_wkgDrawingVisual); _rBmp.Freeze(); _rBmp.CopyPixels(_wallPixels, (_rBmp.PixelWidth * _rBmp.Format.BitsPerPixel + 7) / 8, 0); }
internal BrushTransform(Brush brush, Rect bounds) { ToAbsolute = Matrix.Identity; ToAbsolute.Scale(bounds.Width, bounds.Height); ToAbsolute.Translate(bounds.X, bounds.Y); var fromAbsolute = ToAbsolute; fromAbsolute.Invert(); ToBrush = fromAbsolute * brush.RelativeTransform.Value * ToAbsolute * brush.Transform.Value; if (!ToBrush.HasInverse) { var dv = new DrawingVisual(); using (var dc = dv.RenderOpen()) dc.DrawRectangle(brush, null, new Rect(0, 0, 1, 1)); var rtb = new RenderTargetBitmap(1, 1, 0, 0, PixelFormats.Pbgra32); rtb.Render(dv); var c = new byte[4]; rtb.CopyPixels(c, 4, 0); DegenerateBrush = new d.SolidBrush(d.Color.FromArgb(c[3], c[2], c[1], c[0])); } }
/// <summary> /// The print to png file. /// </summary> /// <param name="canvas1"> /// The canvas 1. /// </param> /// <param name="canvas2"> /// The canvas 2. /// </param> /// <param name="canvas3"> /// The canvas 3. /// </param> /// <param name="canvas4"> /// The canvas 4. /// </param> /// <param name="pngFilePath"> /// The png file path. /// </param> /// <returns> /// The <see cref="BitmapSource"/>. /// </returns> public static BitmapSource PrintToPngFile( Canvas canvas1, Canvas canvas2, Canvas canvas3, Canvas canvas4, string pngFilePath) { var rtb1 = new RenderTargetBitmap( (int)Math.Round(canvas1.ActualWidth, 0), (int)Math.Round(canvas1.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas1); rtb1.Render(canvas1); ModifyPositionBack(canvas1); var pixels1 = new byte[rtb1.PixelWidth * rtb1.PixelHeight * rtb1.Format.BitsPerPixel / 8]; rtb1.CopyPixels(pixels1, (rtb1.PixelWidth * rtb1.Format.BitsPerPixel) / 8, 0); var rtb2 = new RenderTargetBitmap( (int)Math.Round(canvas2.ActualWidth, 0), (int)Math.Round(canvas2.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas2); rtb2.Render(canvas2); ModifyPositionBack(canvas2); var pixels2 = new byte[rtb2.PixelWidth * rtb2.PixelHeight * rtb2.Format.BitsPerPixel / 8]; rtb2.CopyPixels(pixels2, (rtb2.PixelWidth * rtb2.Format.BitsPerPixel) / 8, 0); var rtb3 = new RenderTargetBitmap( (int)Math.Round(canvas3.ActualWidth, 0), (int)Math.Round(canvas3.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas3); rtb3.Render(canvas3); ModifyPositionBack(canvas3); var pixels3 = new byte[rtb3.PixelWidth * rtb3.PixelHeight * rtb3.Format.BitsPerPixel / 8]; rtb3.CopyPixels(pixels3, (rtb3.PixelWidth * rtb3.Format.BitsPerPixel) / 8, 0); var rtb4 = new RenderTargetBitmap( (int)Math.Round(canvas4.ActualWidth, 0), (int)Math.Round(canvas4.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas4); rtb4.Render(canvas4); ModifyPositionBack(canvas4); var pixels4 = new byte[rtb4.PixelWidth * rtb4.PixelHeight * rtb4.Format.BitsPerPixel / 8]; rtb4.CopyPixels(pixels4, (rtb4.PixelWidth * rtb4.Format.BitsPerPixel) / 8, 0); var wb = new WriteableBitmap( rtb1.PixelWidth + rtb2.PixelWidth, rtb1.PixelHeight + rtb3.PixelHeight, 96.0, 96.0, PixelFormats.Pbgra32, null); wb.WritePixels( new Int32Rect(0, 0, rtb1.PixelWidth, rtb1.PixelHeight), pixels1, (rtb1.PixelWidth * rtb1.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(rtb1.PixelWidth, 0, rtb2.PixelWidth, rtb2.PixelHeight), pixels2, (rtb2.PixelWidth * rtb2.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(0, rtb1.PixelHeight, rtb3.PixelWidth, rtb3.PixelHeight), pixels3, (rtb3.PixelWidth * rtb3.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(rtb3.PixelWidth, rtb2.PixelHeight, rtb4.PixelWidth, rtb4.PixelHeight), pixels4, (rtb4.PixelWidth * rtb4.Format.BitsPerPixel) / 8, 0); var en = new PngBitmapEncoder(); en.Frames.Add(BitmapFrame.Create(wb)); var s = new FileStream(pngFilePath, FileMode.OpenOrCreate, FileAccess.Write); en.Save(s); s.Close(); return wb; }
/// <summary> /// The print to printer. /// </summary> /// <param name="canvas1"> /// The canvas 1. /// </param> /// <param name="canvas2"> /// The canvas 2. /// </param> /// <param name="canvas3"> /// The canvas 3. /// </param> /// <param name="canvas4"> /// The canvas 4. /// </param> /// <returns> /// The <see cref="BitmapSource"/>. /// </returns> public static BitmapSource PrintToPrinter(Canvas canvas1, Canvas canvas2, Canvas canvas3, Canvas canvas4) { var rtb1 = new RenderTargetBitmap( (int)Math.Round(canvas1.ActualWidth, 0), (int)Math.Round(canvas1.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas1); rtb1.Render(canvas1); ModifyPositionBack(canvas1); var pixels1 = new byte[rtb1.PixelWidth * rtb1.PixelHeight * rtb1.Format.BitsPerPixel / 8]; rtb1.CopyPixels(pixels1, (rtb1.PixelWidth * rtb1.Format.BitsPerPixel) / 8, 0); var rtb2 = new RenderTargetBitmap( (int)Math.Round(canvas2.ActualWidth, 0), (int)Math.Round(canvas2.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas2); rtb2.Render(canvas2); ModifyPositionBack(canvas2); var pixels2 = new byte[rtb2.PixelWidth * rtb2.PixelHeight * rtb2.Format.BitsPerPixel / 8]; rtb2.CopyPixels(pixels2, (rtb2.PixelWidth * rtb2.Format.BitsPerPixel) / 8, 0); var rtb3 = new RenderTargetBitmap( (int)Math.Round(canvas3.ActualWidth, 0), (int)Math.Round(canvas3.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas3); rtb3.Render(canvas3); ModifyPositionBack(canvas3); var pixels3 = new byte[rtb3.PixelWidth * rtb3.PixelHeight * rtb3.Format.BitsPerPixel / 8]; rtb3.CopyPixels(pixels3, (rtb3.PixelWidth * rtb3.Format.BitsPerPixel) / 8, 0); var rtb4 = new RenderTargetBitmap( (int)Math.Round(canvas4.ActualWidth, 0), (int)Math.Round(canvas4.ActualHeight, 0), 96.0, 96.0, PixelFormats.Pbgra32); ModifyPosition(canvas4); rtb4.Render(canvas4); ModifyPositionBack(canvas4); var pixels4 = new byte[rtb4.PixelWidth * rtb4.PixelHeight * rtb4.Format.BitsPerPixel / 8]; rtb4.CopyPixels(pixels4, (rtb4.PixelWidth * rtb4.Format.BitsPerPixel) / 8, 0); var wb = new WriteableBitmap( rtb1.PixelWidth + rtb2.PixelWidth, rtb1.PixelHeight + rtb3.PixelHeight, 96.0, 96.0, PixelFormats.Pbgra32, null); wb.WritePixels( new Int32Rect(0, 0, rtb1.PixelWidth, rtb1.PixelHeight), pixels1, (rtb1.PixelWidth * rtb1.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(rtb1.PixelWidth, 0, rtb2.PixelWidth, rtb2.PixelHeight), pixels2, (rtb2.PixelWidth * rtb2.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(0, rtb1.PixelHeight, rtb3.PixelWidth, rtb3.PixelHeight), pixels3, (rtb3.PixelWidth * rtb3.Format.BitsPerPixel) / 8, 0); wb.WritePixels( new Int32Rect(rtb3.PixelWidth, rtb2.PixelHeight, rtb4.PixelWidth, rtb4.PixelHeight), pixels4, (rtb4.PixelWidth * rtb4.Format.BitsPerPixel) / 8, 0); var d = new PrintDialog(); if (d.ShowDialog() == true) { var img = new Image(); var b = new Border(); b.Padding = new Thickness(1 * 96.0 / 2.54); b.Child = img; img.Source = wb; b.Measure(new Size(d.PrintableAreaWidth, d.PrintableAreaHeight)); b.Arrange(new Rect(new Point(0, 0), new Size(d.PrintableAreaWidth, d.PrintableAreaHeight))); d.PrintVisual(b, "Print canvases"); } return wb; }
private BitmapSource DrawLines(string transformedText, double scaleX, double scaleY) { var bitmap = new RenderTargetBitmap((int)(LayoutRoot.ActualWidth * scaleX), (int)(LayoutRoot.ActualHeight * scaleY), 96, 96, PixelFormats.Pbgra32); Rect? lastRect = null; var result = new byte[(int)(LayoutRoot.ActualWidth * scaleX) * 4 * (int)(LayoutRoot.ActualHeight * scaleY)]; foreach (var symbol in transformedText) { if (symbol < '1' || symbol > '9') continue; var rect = ConvertNumberToCellCoords(symbol - '1'); rect.X *= scaleX; rect.Y *= scaleY; rect.Width *= scaleX; rect.Height *= scaleY; var color = new Color { A = 255, R = LineColor.SelectedColor.Value.R, G = LineColor.SelectedColor.Value.G, B = LineColor.SelectedColor.Value.B }; if (lastRect != null) { if (lastRect == rect) { var canvas = new Canvas(); var circle = new Ellipse { Stroke = new SolidColorBrush(color), StrokeThickness = (LineSize.Value + 2) * (scaleX + scaleY) / 2 }; circle.Width = (LineSize.Value + 2) * 2 * scaleX; circle.Height = (LineSize.Value + 2) * 2 * scaleY; Canvas.SetLeft(circle, rect.Width / 2 - (LineSize.Value + 2) * scaleX + rect.Left); Canvas.SetTop(circle, rect.Height / 2 - (LineSize.Value + 2) * scaleY + rect.Top); canvas.Children.Add(circle); canvas.Measure(new Size(rect.Width, rect.Height)); canvas.Arrange(new Rect(new Size(rect.Width, rect.Height))); canvas.UpdateLayout(); bitmap.Render(canvas); } else { var lineLength = Math.Sqrt( Math.Abs(rect.Left - lastRect.Value.Left) * Math.Abs(rect.Left - lastRect.Value.Left) + Math.Abs(rect.Top - lastRect.Value.Top) * Math.Abs(rect.Top - lastRect.Value.Top)); var proportion = lineLength / Math.Sin(Math.PI / 180 * 90); var angle = Math.Asin(Math.Abs(rect.Top - lastRect.Value.Top) / proportion) * (180 / Math.PI); if (rect.Left > lastRect.Value.Left && rect.Top == lastRect.Value.Top) { angle += 90; } if (rect.Left < lastRect.Value.Left && rect.Top == lastRect.Value.Top) { angle -= 90; } if (rect.Left == lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle += 90; } if (rect.Left == lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle -= 90; } if (rect.Left > lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle += 90; } if (rect.Left > lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle = Math.Asin(Math.Abs(rect.Left - lastRect.Value.Left) / proportion) * (180 / Math.PI); } if (rect.Left < lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle -= 90; } if (rect.Left < lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle = Math.Asin(Math.Abs(rect.Left - lastRect.Value.Left) / proportion) * (180 / Math.PI); angle -= 180; } var canvas = new Canvas(); var line = new Line { Stroke = new SolidColorBrush(color), StrokeThickness = LineSize.Value * (scaleX + scaleY) / 2, StrokeStartLineCap = PenLineCap.Round, StrokeEndLineCap = PenLineCap.Round, }; line.X1 = rect.Width / 2; line.Y1 = rect.Height / 2; line.X2 = rect.Width / 2; line.Y2 = rect.Height / 2 + lineLength; canvas.Children.Add(line); canvas.RenderTransformOrigin = new Point(0.5, 0.5); var transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform(angle)); transformGroup.Children.Add(new TranslateTransform(rect.Left, rect.Top)); canvas.RenderTransform = transformGroup; canvas.Measure(new Size(rect.Width, rect.Height)); canvas.Arrange(new Rect(new Size(rect.Width, rect.Height))); canvas.UpdateLayout(); bitmap.Render(canvas); } } lastRect = rect; } bitmap.CopyPixels( result, (int)(LayoutRoot.ActualWidth * scaleX) * 4, 0); for (var offset = 0; offset < result.Length; offset += 4) { if (result[offset + 3] == 0) continue; result[offset] = (byte)Math.Round(result[offset] / (result[offset + 3] / 255.0)); result[offset + 1] = (byte)Math.Round(result[offset + 1] / (result[offset + 3] / 255.0)); result[offset + 2] = (byte)Math.Round(result[offset + 2] / (result[offset + 3] / 255.0)); } for (var offset = 0; offset < result.Length; offset += 4) { if (result[offset + 3] == 0) continue; result[offset + 3] = Math.Min((byte)(result[offset + 3] * (LineColor.SelectedColor.Value.A / 255.0)), (byte)255); } return BitmapSource.Create( (int)(LayoutRoot.ActualWidth * scaleX), (int)(LayoutRoot.ActualHeight * scaleY), 96, 96, PixelFormats.Bgra32, null, result, (int)(LayoutRoot.ActualWidth * scaleX) * 4); }
private void GetVideoFrame(ref int[] data) { RenderTargetBitmap bmp = new RenderTargetBitmap(videoElement.DesiredPixelWidth, videoElement.DesiredPixelHeight, 96, 96, PixelFormats.Default); bmp.Render(videoElement); bmp.CopyPixels(data, 4 * _width, 0); }
public static BitmapSource VisualToBitmap(Visual e, int Width, int Height, GrayscaleParameters parameters) { Contract.Requires(e != null); e.VerifyAccess(); RenderTargetBitmap src = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32); src.Render(e); if (parameters != null) { byte[] pixels = new byte[Width * Height * 4]; src.CopyPixels(pixels, (Width * 4), 0); for (int p = 0; p < pixels.Length; p += 4) { // compute grayscale double pixelvalue = (((double)pixels[p + 0] * parameters.RedDistribution) + ((double)pixels[p + 1] * parameters.GreenDistribution) + ((double)pixels[p + 2] * parameters.BlueDistribution)); // compute compression pixelvalue = (pixelvalue * parameters.Compression) + (256 * ((1 - parameters.Compression) / 2)); // compute washout pixelvalue = Math.Min(256, pixelvalue + (256 * parameters.Washout)); byte v = (byte)pixelvalue; pixels[p + 0] = v; pixels[p + 1] = v; pixels[p + 2] = v; } return BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Bgr32, null, pixels, Width * 4); } else { return src; } }
public void saveImageFrame() { // For memory testing when not using kinect WriteableBitmap currentFrameCopy; currentFrameCopy = new WriteableBitmap((int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight, 96, 96, PixelFormats.Pbgra32, null); currentFrameCopy.Lock(); RenderTargetBitmap rendrBMP = new RenderTargetBitmap((int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight, 96, 96, PixelFormats.Pbgra32); rendrBMP.Render(kiddieHolder); rendrBMP.CopyPixels(new Int32Rect(0, 0, (int)kiddieHolder.ActualWidth, (int)kiddieHolder.ActualHeight), currentFrameCopy.BackBuffer, currentFrameCopy.BackBufferStride * currentFrameCopy.PixelHeight, currentFrameCopy.BackBufferStride); currentFrameCopy.Unlock(); currentFrameCopy.Freeze(); _photoSubmitter.encodeAndSubmitUserPhotoToCMS(currentFrameCopy); // Wierd WPF bug maybe messes up the visual brush. We can fix it just by doing this if (blurMaskVisualBrush != null) { blurMaskVisualBrush.Visual = null; blurMaskVisualBrush.Visual = _blurMaskSource; } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is FrameworkElement) { FrameworkElement e = (FrameworkElement)value; if (e.ActualHeight == 0 || e.ActualWidth == 0) return null; RenderTargetBitmap src = new RenderTargetBitmap((int)e.ActualWidth, (int)e.ActualHeight, 96, 96, PixelFormats.Pbgra32); src.Render(e); if (ConvertToGrayscale) { byte[] pixels = new byte[(int)src.Width * (int)src.Height * 4]; src.CopyPixels(pixels, ((int)src.Width * 4), 0); for (int p = 0; p < pixels.Length; p += 4) { double val = (((double)pixels[p + 0] * _RedDistribution) + ((double)pixels[p + 1] * _GreenDistribution) + ((double)pixels[p + 2] * _BlueDistribution)); val = (val * Compression) + (256 * ((1 - Compression) / 2)); byte v = (byte)val; pixels[p + 0] = v; pixels[p + 1] = v; pixels[p + 2] = v; } return BitmapSource.Create((int)src.Width, (int)src.Height, 96, 96, PixelFormats.Bgr32, null, pixels, (int)src.Width * 4); } else { return src; } } return null; }
/// <summary> /// Calculates thumbnail. /// TODO: make stable fix bugs rehaul code **save planet** #$^*(*di76arm 7665764bomb8888111~~~!!! msg 78&*%*% over563345 /// </summary> /// <param name="visual"></param> /// <param name="dpi"></param> public void SetImage(Visual visual, Size dpi) { Rect bounds = VisualTreeHelper.GetDescendantBounds(visual); int w = (int)(bounds.Width * dpi.Width / 96.0); int h = (int)(bounds.Height * dpi.Height / 96.0); RenderTargetBitmap rtb = new RenderTargetBitmap(w, h , dpi.Width, dpi.Height, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(visual); ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size)); } rtb.Render(dv); _ThumbnailData = new byte[120 * 90 * 4]; byte[] temp = new byte[w * h * 4]; rtb.CopyPixels(temp, w * 4, 0); MemoryStream ms = new MemoryStream(); BitmapImage bi = new BitmapImage(); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(ms); ms.Seek(0, SeekOrigin.Begin); byte[] result = new byte[ms.Length]; BinaryReader br = new BinaryReader(ms); br.Read(result, 0, (int)ms.Length); br.Close(); ms.Close(); bi.BeginInit(); //bi.DecodePixelHeight = 90; bi.DecodePixelWidth = 120; bi.StreamSource = new MemoryStream(result); bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat; bi.CacheOption = BitmapCacheOption.Default; bi.EndInit(); bi.CopyPixels(this._ThumbnailData, 120 * 4, 0); this._Thumbnail = rtb; }
private static void GenerateWhiteCirclesImage(int width, int height, int[] pixels) { DrawingImage image = new DrawingImage(); Random rnd = new Random(); const double radius = 4; const int circlesNum = 100; DrawingGroup group = new DrawingGroup(); var dc = group.Open(); for (int i = 0; i < circlesNum; i++) { dc.DrawEllipse(Brushes.White, null, new Point(rnd.NextDouble() * width, rnd.NextDouble() * height), radius, radius); } dc.Close(); image.Drawing = group; RenderTargetBitmap renderBmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); renderBmp.Render(new Image { Source = image }); renderBmp.CopyPixels(pixels, (width * renderBmp.Format.BitsPerPixel + 7) / 8, 0); }
private BitmapSource DrawGlow(string transformedText, double scaleX, double scaleY) { var bitmap = new RenderTargetBitmap((int)(LayoutRoot.ActualWidth * scaleX), (int)(LayoutRoot.ActualHeight * scaleY), 96, 96, PixelFormats.Pbgra32); Rect? lastRect = null; var glowBrush = new ImageBrush { Opacity = 0.9, ImageSource = new BitmapImage(new Uri(Path.Combine(Directory.GetCurrentDirectory(), "glow.png"))) }; var glowBrushCircle = new ImageBrush { ImageSource = new BitmapImage(new Uri(Path.Combine(Directory.GetCurrentDirectory(), "glow.png"))) }; glowBrushCircle.Stretch = Stretch.UniformToFill; var transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(1.2, 1)); transformGroup.Children.Add(new TranslateTransform(-4 * (scaleX + scaleY) / 2, 0)); glowBrushCircle.Transform = transformGroup; var result = new byte[(int)(LayoutRoot.ActualWidth * scaleX) * 4 * (int)(LayoutRoot.ActualHeight * scaleY)]; foreach (var symbol in transformedText) { if (symbol < '1' || symbol > '9') continue; var rect = ConvertNumberToCellCoords(symbol - '1'); rect.X *= scaleX; rect.Y *= scaleY; rect.Width *= scaleX; rect.Height *= scaleY; if (lastRect != null) { if (lastRect == rect) { // Glow var canvas = new Canvas(); var circle = new Ellipse { Stroke = glowBrushCircle, StrokeThickness = (GlowSize.Value - 3.5) * (scaleX + scaleY) / 2 }; circle.Width = (GlowSize.Value - 3.5) * 2 * scaleX; circle.Height = (GlowSize.Value - 3.5) * 2 * scaleY; circle.Opacity = 0.35; Canvas.SetLeft(circle, rect.Width / 2 - (GlowSize.Value - 3.5) * scaleX + rect.Left); Canvas.SetTop(circle, rect.Height / 2 - (GlowSize.Value - 3.5) * scaleY + rect.Top); canvas.Children.Add(circle); var circle2 = new Ellipse { Stroke = glowBrushCircle, StrokeThickness = (GlowSize.Value - 3) * (scaleX + scaleY) / 2 }; circle2.Width = (GlowSize.Value - 3) * 2 * scaleX; circle2.Height = (GlowSize.Value - 3) * 2 * scaleY; circle2.Opacity = 0.2; Canvas.SetLeft(circle2, rect.Width / 2 - (GlowSize.Value - 3) * scaleX + rect.Left); Canvas.SetTop(circle2, rect.Height / 2 - (GlowSize.Value - 3) * scaleY + rect.Top); canvas.Children.Add(circle2); var circle3 = new Ellipse { Stroke = glowBrushCircle, StrokeThickness = (GlowSize.Value - 2.5) * (scaleX + scaleY) / 2 }; circle3.Width = (GlowSize.Value - 2.5) * 2 * scaleX; circle3.Height = (GlowSize.Value - 2.5) * 2 * scaleY; circle3.Opacity = 0.5; Canvas.SetLeft(circle3, rect.Width / 2 - (GlowSize.Value - 2.5) * scaleX + rect.Left); Canvas.SetTop(circle3, rect.Height / 2 - (GlowSize.Value - 2.5) * scaleY + rect.Top); canvas.Children.Add(circle3); canvas.Measure(new Size(rect.Width, rect.Height)); canvas.Arrange(new Rect(new Size(rect.Width, rect.Height))); canvas.UpdateLayout(); bitmap.Render(canvas); } else { var lineLength = Math.Sqrt( Math.Abs(rect.Left - lastRect.Value.Left) * Math.Abs(rect.Left - lastRect.Value.Left) + Math.Abs(rect.Top - lastRect.Value.Top) * Math.Abs(rect.Top - lastRect.Value.Top)); var proportion = lineLength / Math.Sin(Math.PI / 180 * 90); var angle = Math.Asin(Math.Abs(rect.Top - lastRect.Value.Top) / proportion) * (180 / Math.PI); if (rect.Left > lastRect.Value.Left && rect.Top == lastRect.Value.Top) { angle += 90; } if (rect.Left < lastRect.Value.Left && rect.Top == lastRect.Value.Top) { angle -= 90; } if (rect.Left == lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle += 90; } if (rect.Left == lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle -= 90; } if (rect.Left > lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle += 90; } if (rect.Left > lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle = Math.Asin(Math.Abs(rect.Left - lastRect.Value.Left) / proportion) * (180 / Math.PI); } if (rect.Left < lastRect.Value.Left && rect.Top < lastRect.Value.Top) { angle -= 90; } if (rect.Left < lastRect.Value.Left && rect.Top > lastRect.Value.Top) { angle = Math.Asin(Math.Abs(rect.Left - lastRect.Value.Left) / proportion) * (180 / Math.PI); angle -= 180; } // Glow var canvas = new Canvas(); var line = new Line { Stroke = glowBrush, StrokeThickness = GlowSize.Value * (scaleX + scaleY) / 2, StrokeStartLineCap = PenLineCap.Round, StrokeEndLineCap = PenLineCap.Round, }; line.X1 = rect.Width / 2; line.Y1 = rect.Height / 2; line.X2 = rect.Width / 2; line.Y2 = rect.Height / 2 + lineLength; line.Opacity = 0.5; canvas.Children.Add(line); var line2 = new Line { Stroke = glowBrush, StrokeThickness = (GlowSize.Value + 0.5) * (scaleX + scaleY) / 2, StrokeStartLineCap = PenLineCap.Round, StrokeEndLineCap = PenLineCap.Round, }; line2.X1 = rect.Width / 2; line2.Y1 = rect.Height / 2 - 0.5 * (scaleX + scaleY) / 2; line2.X2 = rect.Width / 2; line2.Y2 = rect.Height / 2 + lineLength + 0.5 * (scaleX + scaleY) / 2; line2.Opacity = 0.35; canvas.Children.Add(line2); var line3 = new Line { Stroke = glowBrush, StrokeThickness = (GlowSize.Value + 1) * (scaleX + scaleY) / 2, StrokeStartLineCap = PenLineCap.Round, StrokeEndLineCap = PenLineCap.Round, }; line3.X1 = rect.Width / 2; line3.Y1 = rect.Height / 2 - 1 * (scaleX + scaleY) / 2; line3.X2 = rect.Width / 2; line3.Y2 = rect.Height / 2 + lineLength + 1 * (scaleX + scaleY) / 2; line3.Opacity = 0.15; canvas.Children.Add(line3); canvas.RenderTransformOrigin = new Point(0.5, 0.5); transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform(angle)); transformGroup.Children.Add(new TranslateTransform(rect.Left, rect.Top)); canvas.RenderTransform = transformGroup; canvas.Measure(new Size(rect.Width, rect.Height)); canvas.Arrange(new Rect(new Size(rect.Width, rect.Height))); canvas.UpdateLayout(); bitmap.Render(canvas); // } } lastRect = rect; } bitmap.CopyPixels( result, (int)(LayoutRoot.ActualWidth * scaleX) * 4, 0); for (var offset = 0; offset < result.Length; offset += 4) { if (result[offset + 3] == 0) continue; result[offset] = (byte)Math.Round(result[offset] / (result[offset + 3] / 255.0)); result[offset + 1] = (byte)Math.Round(result[offset + 1] / (result[offset + 3] / 255.0)); result[offset + 2] = (byte)Math.Round(result[offset + 2] / (result[offset + 3] / 255.0)); } for (var offset = 0; offset < result.Length; offset += 4) { if (result[offset + 3] == 0) continue; result[offset] = Math.Min((byte)(result[offset] * (GlowColor.SelectedColor.Value.B / 255.0)), (byte)255); result[offset + 1] = Math.Min((byte)(result[offset + 1] * (GlowColor.SelectedColor.Value.G / 255.0)), (byte)255); result[offset + 2] = Math.Min((byte)(result[offset + 2] * (GlowColor.SelectedColor.Value.R / 255.0)), (byte)255); result[offset + 3] = Math.Min((byte)(result[offset + 3] * (GlowColor.SelectedColor.Value.A / 255.0)), (byte)255); } return BitmapSource.Create( (int)(LayoutRoot.ActualWidth * scaleX), (int)(LayoutRoot.ActualHeight * scaleY), 96, 96, PixelFormats.Bgra32, null, result, (int)(LayoutRoot.ActualWidth * scaleX) * 4); }