Esempio n. 1
0
        /* public static bool Fits(this WriteableBitmap wbm, Bitmap bmp)
         * {
         *   if (wbm.ToInt32Rect().)
         *       return true;
         *   else
         *       return false;
         * }*/


        public static WriteableBitmap ToWriteableBitmap(this BitmapSource bms)
        {
            if (bms.IsNullOrEmpty())
            {
                return(null);
            }

            int stride = bms.GetStride();

            byte[] data = bms.ToPixelsByteArray();

            WriteableBitmap wbm = new WriteableBitmap(bms.PixelWidth, bms.PixelHeight, bms.DpiX, bms.DpiY, bms.Format, null);

            wbm.WritePixels(bms.ToInt32Rect(), data, stride, 0);

            return(wbm);
        }
        public static Bitmap ToBitmapFast(this BitmapSource source, System.Drawing.Imaging.PixelFormat format)
        {
            if (source.IsNullOrEmpty())
            {
                return(null);
            }

            Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, format);

            BitmapData data = bmp.LockBits(bmp.ToRectangle(), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);// format);

            source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);

            bmp.UnlockBits(data);

            return(bmp);
        }
        public static Bitmap ToBitmap(this BitmapSource source, BitmapFrameEx.BitmapFrameFormatMode mode)
        {
            if (source.IsNullOrEmpty())
            {
                return(null);
            }

            Bitmap result;

            using (MemoryStream stream = new MemoryStream())
            {
                BitmapEncoder encoder = mode.GetBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(stream);
                result = new Bitmap(stream);
            }

            return(result);
        }
Esempio n. 4
0
        public static BitmapImage ToBitmapImage(this BitmapSource bms)
        {
            if (bms.IsNullOrEmpty())
            {
                return(null);
            }

            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            BitmapImage       bmi     = new BitmapImage();

            using (MemoryStream stream = new MemoryStream())
            {
                encoder.Frames.Add(BitmapFrame.Create(bms));
                encoder.Save(stream);

                bmi.BeginInit();
                bmi.StreamSource = new MemoryStream(stream.ToArray());
                bmi.EndInit();
            }

            return(bmi);
        }