Ejemplo n.º 1
0
 public static int[] GetPixels(System.Windows.Media.Imaging.BitmapImage imageSource)
 {
     System.Windows.Media.Imaging.WriteableBitmap wbmp = new System.Windows.Media.Imaging.WriteableBitmap(imageSource);
     int[] pixels = new int[wbmp.PixelWidth * wbmp.PixelHeight];
     wbmp.CopyPixels(pixels, wbmp.PixelWidth * 4, 0);
     return(pixels);
 }
Ejemplo n.º 2
0
 public static int[] GetPixels(System.Windows.Media.Imaging.BitmapImage imageSource)
 {
     System.Windows.Media.Imaging.WriteableBitmap wbmp = new System.Windows.Media.Imaging.WriteableBitmap(imageSource);
     int[] pixels = new int[wbmp.PixelWidth * wbmp.PixelHeight];
     wbmp.CopyPixels(pixels, wbmp.PixelWidth * 4, 0);
     return pixels;
 }
Ejemplo n.º 3
0
        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;
        }