コード例 #1
0
        private void InsertMessageAction(object parameter)
        {
            string message = parameter as string;

            MemoryStream ms = new MemoryStream();
            BitmapEncoder encode = new BmpBitmapEncoder();

            encode.Frames.Add(BitmapFrame.Create(_sourceImage));
            encode.Save(ms);
            var bmpData = ms.ToArray();
            BitmapManipulator manipulator = new BitmapManipulator(bmpData);
            string encryptedMessage = Cryptography.Encrypt(message, this.Password);
            manipulator.InsertMessage(encryptedMessage);

            var imageSource = new BitmapImage();
            MemoryStream imageMS = new MemoryStream(manipulator.Bytes);

            imageSource.BeginInit();
            imageSource.StreamSource = imageMS;
            imageSource.EndInit();

            CryptedImage = imageSource;
            if (bmpData.Length != manipulator.Bytes.Length)
            {
                Debugger.Break();
            }

        }
コード例 #2
0
        public static void Initialize(dynamic window)
        {
            Bitmap icon;
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(new Uri(@"pack://*****:*****@"Exit"};
            tsm.Click += (sender, e) =>
            {
                Exit();
            };
            cms.Items.Add(tsm);
            _notifyIcon.ContextMenuStrip = cms;

            Window = window;
        }
コード例 #3
0
ファイル: WorkImage.cs プロジェクト: noedelarosa/SIC
        public static bool ToFile(BitmapSource source, string s_uri)
        {
            if (source != null)
            {
                System.IO.File.Delete(s_uri);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                BmpBitmapEncoder bmpenco = new BmpBitmapEncoder();
                bmpenco.Frames.Add(BitmapFrame.Create(source));
                bmpenco.Save(ms);
                ms.Flush();
                
                System.IO.Stream st = System.IO.File.Open(s_uri, System.IO.FileMode.CreateNew);
                st.Write(ms.GetBuffer(), 0, ms.ToArray().Length);
                
                st.Close();
                st.Dispose();

                ms.Dispose();
                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #4
0
ファイル: PdfDocumentCreator.cs プロジェクト: LexaGal/Plants
        public Document CreatePdfDocument(Chart chart, string path)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
                (int) chart.ActualWidth,
                (int) chart.ActualHeight,
                96d,
                96d,
                PixelFormats.Pbgra32);

            renderBitmap.Render(chart);

            MemoryStream stream = new MemoryStream();
            BitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            encoder.Save(stream);

            Bitmap bitmap = new Bitmap(stream);
            System.Drawing.Image image = bitmap;

            System.Drawing.Image resizedImage = ResizeImage(image, image.Width*2, image.Height);

            Document doc = new Document(PageSize.A4);
            PdfWriter.GetInstance(doc, new FileStream(path, FileMode.OpenOrCreate));
            doc.Open();
            Image pdfImage = Image.GetInstance(resizedImage, ImageFormat.Jpeg);
            doc.Add(pdfImage);
            doc.Close();
            
            return doc;
        }
コード例 #5
0
        public static void Save(this BitmapSource image, string filePath, ImageFormat format)
        {
            BitmapEncoder encoder = null;
            
            switch(format)
            {
                case ImageFormat.Png:
                    encoder = new PngBitmapEncoder();
                    break;
                case ImageFormat.Jpeg:
                    encoder = new JpegBitmapEncoder();
                    break;
                case ImageFormat.Bmp:
                    encoder = new BmpBitmapEncoder();
                    break;
            }

            if (encoder == null) 
                return;

            encoder.Frames.Add(BitmapFrame.Create(BitmapFrame.Create(image)));

            using (var stream = new FileStream(filePath, FileMode.Create))
                encoder.Save(stream);
        }
コード例 #6
0
ファイル: UIHelper.cs プロジェクト: sipsorcery/sipsorcery
        public static BitmapImage Bitmap2BitmapImage(System.Drawing.Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            BitmapSource bmpSource;
            BitmapImage bitmapImage = new BitmapImage();

            try
            {
                bmpSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                             hBitmap,
                             IntPtr.Zero,
                             Int32Rect.Empty,
                             BitmapSizeOptions.FromEmptyOptions());

                BitmapEncoder enc = new BmpBitmapEncoder();
                MemoryStream memoryStream = new MemoryStream();

                enc.Frames.Add(BitmapFrame.Create(bmpSource));
                enc.Save(memoryStream);

                memoryStream.Position = 0;
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
            }
            finally
            {
                DeleteObject(hBitmap);
            }

            return bitmapImage;
        }
コード例 #7
0
ファイル: BitmapHelper.cs プロジェクト: enesyteam/EChess
        public static System.Drawing.Bitmap BMPFromBitmapSource(BitmapSource source, int iCount)
        {
            //int width = 128;
            //int height = 128;
            //int stride = width;
            //byte[] pixels = new byte[height * stride];

            //// Define the image palette
            //BitmapPalette myPalette = BitmapPalettes.Halftone256;

            //// Creates a new empty image with the pre-defined palette

            //BitmapSource image = BitmapSource.Create(
            //    width,
            //    height,
            //    96,
            //    96,
            //    PixelFormats.Indexed8,
            //    myPalette,
            //    pixels,
            //    stride);

            FileStream stream = new FileStream("image" + iCount + ".BMP", FileMode.Create);
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            //TextBlock myTextBlock = new TextBlock();
            //myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
            //encoder.Interlace = PngInterlaceOption.On;
            encoder.Frames.Add(BitmapFrame.Create(source));
            encoder.Save(stream);

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream);
            return bitmap;
        }
コード例 #8
0
        private void InsertImageIntoRange(ImageSource source, Excel.Worksheet currentSheet, Excel.Range range, int maxHeight = -1)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(source as System.Windows.Media.Imaging.BitmapSource));
            encoder.Save(ms);
            ms.Flush();

            System.Windows.Forms.Clipboard.SetDataObject(System.Drawing.Image.FromStream(ms), true);
            currentSheet.Paste(range);

            // Resize image to fit range
            Excel.Shape shape = (Excel.Shape)currentSheet.Shapes.Item(currentSheet.Shapes.Count);
            shape.LockAspectRatio = Office.MsoTriState.msoCTrue;

            if (maxHeight == -1)
            {
                shape.Height = (int)range.Height;
            }
            else
            {
                shape.Height = (int)((source.Height / (double)maxHeight) * range.Height);
            }

            shape.Line.Visible = Office.MsoTriState.msoTrue;
            ms.Close();
        }
コード例 #9
0
ファイル: ImageToBinaryConverter.cs プロジェクト: vnl/Monies
        /// <summary>
        /// Converts ImageSource to byte[]
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var renderTargetBitmap = value as RenderTargetBitmap;
            if (null != renderTargetBitmap)
            {
                var bitmapImage = new BitmapImage();
                var bitmapEncoder = new BmpBitmapEncoder();
                bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

                using (var stream = new MemoryStream())
                {
                    bitmapEncoder.Save(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    bitmapImage.BeginInit();
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage.StreamSource = stream;
                    bitmapImage.EndInit();
                }
                var encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
                byte[] data = null;
                using (var ms = new MemoryStream())
                {
                    encoder.Save(ms);
                    data = ms.ToArray();
                }
                return data;
            }
            return null;
        }
コード例 #10
0
        private BitmapSource RefreshZoomedImage(System.Windows.Point pt)
        {
            using (var screenBmp = new Bitmap(
                       80,
                       80,
                       System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                MemoryStream ms      = new MemoryStream();
                var          encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(this.screenImage.Source as System.Windows.Media.Imaging.BitmapSource));
                encoder.Save(ms);
                ms.Flush();

                using (var bmpGraphics = Graphics.FromImage(System.Drawing.Image.FromStream(ms)))
                {
                    bmpGraphics.CopyFromScreen((int)pt.X, (int)pt.Y, 0, 0, screenBmp.Size);
                    return(Imaging.CreateBitmapSourceFromHBitmap(
                               screenBmp.GetHbitmap(),
                               IntPtr.Zero,
                               Int32Rect.Empty,
                               BitmapSizeOptions.FromEmptyOptions()));
                }
            }

            //CroppedBitmap cp = new CroppedBitmap(this.screenImage.Source as BitmapSource, new Int32Rect((int)pt.X, (int)pt.Y, 80, 80));
            //cp.
        }
コード例 #11
0
ファイル: ImageUtil.cs プロジェクト: vincenthamm/ATF
 /// <summary>
 /// Renders a visual UI element to a stream in bitmap (BMP) format</summary>
 /// <param name="visual">Visual UI element to be rendered</param>
 /// <param name="stream">Stream data written to</param>
 /// <param name="scale">Rendering scale</param>
 public static void RenderToBmp(this UIElement visual, Stream stream, double scale)
 {
     BitmapFrame frame = visual.RenderToBitmapFrame(scale);
     var encoder = new BmpBitmapEncoder();
     encoder.Frames.Add(frame);
     encoder.Save(stream);
 }
コード例 #12
0
ファイル: MainWindow.xaml.cs プロジェクト: fumimi2you/Mason
        private void LoadImage(string imgPathOrg)
        {
            BitmapImage bmpImgPrc = new BitmapImage(); // デコードされたビットマップイメージのインスタンスを作る。

            try
            {
                // 一度解像度を整えて、固定パスに出力
                System.Drawing.Bitmap bmp = null;
                {
                    BitmapImage bmpImgSrc = new BitmapImage();
                    bmpImgSrc.BeginInit();
                    bmpImgSrc.UriSource = new Uri(imgPathOrg); // ビットマップイメージのソースにファイルを指定する。
                    bmpImgSrc.EndInit();

                    var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmpImgSrc));
                    using (var ms = new System.IO.MemoryStream())
                    {
                        encoder.Save(ms);
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
                        using (var temp = new System.Drawing.Bitmap(ms))
                        {
                            // このおまじないの意味は参考資料を参照
                            bmp = new System.Drawing.Bitmap(temp);
                        }
                    }
                }
                // 解像度設定
                bmp.SetResolution(96, 96);

                // 一度固定ファイルとして保存
                imgPath = System.IO.Path.GetDirectoryName(imgPathOrg) + "\\_tmp.jpg";
                bmp.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                bmp.Dispose();

                //  再読込
                bmpImgPrc.BeginInit();
                bmpImgPrc.UriSource = new Uri(imgPath); // ビットマップイメージのソースにファイルを指定する。
                bmpImgPrc.EndInit();


                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = bmpImgPrc;
                imageBrush.Stretch     = Stretch.None;
                imageBrush.AlignmentX  = AlignmentX.Left;
                imageBrush.AlignmentY  = AlignmentY.Top;

                imgSize.Width  = bmpImgPrc.PixelWidth;
                imgSize.Height = bmpImgPrc.PixelHeight;

                canvas.Background = imageBrush; // Imageコントロールにバインディングする。

                this.Title = System.IO.Path.GetFileName(imgPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #13
0
        public static void ExportToImage(FrameworkElement surface)
        {
            Size size = new Size(surface.ActualWidth, surface.ActualHeight);
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
            renderBitmap.Render(surface);

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = DateTime.Now.ToString("yyyy-M-d-H_m_s");
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\WLANFolder";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            dlg.InitialDirectory = path;
            dlg.OverwritePrompt = true;
            dlg.Filter = "JPEG Image|*.jpg|Bitmap Image|*.bmp|PNG Image|*.png";
            dlg.Title = "Save an Image File";
            dlg.ShowDialog();
            dlg.RestoreDirectory = true;

            if (dlg.FileName != "")
            {
                try
                {
                    using(FileStream outStream = (System.IO.FileStream)dlg.OpenFile())
                    {
                        switch (dlg.FilterIndex)
                        {
                            case 1:
                                {
                                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                                    encoder.Save(outStream);
                                }
                                break;
                            case 2:
                                {
                                    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                                    encoder.Save(outStream);
                                }
                                break;
                            case 3:
                                {
                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                                    encoder.Save(outStream);
                                }
                                break;
                            default:
                                break;
                        }
                        System.Diagnostics.Process.Start(dlg.FileName);
                    }          
                }
                catch (ArgumentException)
                { return; }
            }
        }
コード例 #14
0
ファイル: ImageSaver.cs プロジェクト: mk1234444/TwitchAlert
 /// <summary>
 /// Save the BitmapImage img to the File filename as a .bmp
 /// </summary>
 /// <param name="img"></param>
 /// <param name="fileName"></param>
 public static void SaveToBmp(BitmapImage img, string fileName)
 {
     var encoder = new BmpBitmapEncoder();
     encoder.Frames.Add(BitmapFrame.Create(img));
     using (var filestream = new FileStream(fileName, FileMode.Create))
     {
         encoder.Save(filestream);
     }
 }
コード例 #15
0
        public static Stream ToStream(BitmapSource bitmap)
        {
            var memory = new MemoryStream();

            var encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            encoder.Save(memory);

            return memory;
        }
コード例 #16
0
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            MemoryStream ms = new MemoryStream();
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create((BitmapImage)value));
            enc.Save(ms);
            Bitmap bitmap = new Bitmap(ms);

            return new Bitmap(bitmap);
        }
コード例 #17
0
 public static Bitmap BitmapFromSource(this BitmapSource bitmapsource)
 {
     // do not close stream cuz bitmap become invalid.
     // TODO: possible memory leakage? dunno. investigate.
     var outStream = new MemoryStream();
     BitmapEncoder enc = new BmpBitmapEncoder();
     enc.Frames.Add(BitmapFrame.Create(bitmapsource));
     enc.Save(outStream);
     return new Bitmap(outStream);
 }
コード例 #18
0
        private static MemoryStream GetBmpStream(BitmapFrame photo)
        {
            MemoryStream result = new MemoryStream();

            BmpBitmapEncoder targetEncoder = new BmpBitmapEncoder();
            targetEncoder.Frames.Add(photo);
            targetEncoder.Save(result);

            return result;
        }
コード例 #19
0
 //-------------------------------------
 private Bitmap BitmapImage2Bitmap(ref BitmapImage bitmapImage)
 {
     using (MemoryStream outStream = new MemoryStream())
     {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create(bitmapImage));
         enc.Save(outStream);
         return new Bitmap(outStream);
     }
 }
コード例 #20
0
ファイル: ImageHelper.cs プロジェクト: steamprodz/DantistApp
        public static System.Drawing.Image ImageWpfToGDI(System.Windows.Media.ImageSource imageSource)
        {
            MemoryStream ms = new MemoryStream();
            var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(imageSource as System.Windows.Media.Imaging.BitmapSource));
            encoder.Save(ms);
            ms.Flush();

            return System.Drawing.Image.FromStream(ms);
        }
 /// <summary>
 /// Converts the given BitmapSource into a Bitmap.
 /// </summary>
 /// <param name="This">This BitmapSource.</param>
 /// <returns>The copy of the BitmapSource as a Bitmap-Instance.</returns>
 public static Bitmap AsBitmap(this BitmapSource This) {
   Contract.Requires(This != null);
   using (var memoryStream = new IO.MemoryStream()) {
     var bitmapEncoder = new BmpBitmapEncoder();
     bitmapEncoder.Frames.Add(BitmapFrame.Create(This));
     bitmapEncoder.Save(memoryStream);
     using (var temp = new Bitmap(memoryStream))
       return (new Bitmap(temp));
   }
 }
コード例 #22
0
ファイル: Core.cs プロジェクト: perfectdev/SkinDesigner
 public static Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
 {
     using (var outStream = new MemoryStream()) {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create(bitmapImage));
         enc.Save(outStream);
         var bitmap = new Bitmap(outStream);
         return new Bitmap(bitmap);
     }
 }
コード例 #23
0
        private System.Drawing.Image ImageWpfToGDI(BitmapSource image)
        {
            MemoryStream ms      = new MemoryStream();
            var          encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image));
            encoder.Save(ms);
            ms.Flush();
            return(System.Drawing.Image.FromStream(ms));
        }
コード例 #24
0
        public static Bitmap Convert(BitmapImage bitmap)
        {
            //TODO: фэйл какойто
            var memoryStream = new MemoryStream();
            var bitmapEncoder = new BmpBitmapEncoder();

            bitmapEncoder.Frames.Add(BitmapFrame.Create(memoryStream));
            bitmapEncoder.Save(memoryStream);

            return new Bitmap(memoryStream);
        }
コード例 #25
0
ファイル: ImageHelper.cs プロジェクト: shimins/Eyetracking
 public static Bitmap GetBitmap(BitmapSource bitmapImage)
 {
     using (var memory = new MemoryStream())
     {
         var bitmapEncoder = new BmpBitmapEncoder();
         bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapImage));
         bitmapEncoder.Save(memory);
         var bitmap = new Bitmap(memory);
         return new Bitmap(bitmap);
     }
 }
コード例 #26
0
 private static System.Drawing.Image ImageSourceToGDI(BitmapSource src)
 {
     using (var ms = new MemoryStream())
     {
         var encoder = new BmpBitmapEncoder();
         encoder.Frames.Add(BitmapFrame.Create(src));
         encoder.Save(ms);
         ms.Flush();
         return System.Drawing.Image.FromStream(ms);
     }
 }
コード例 #27
0
 /// <summary>
 ///   Create a System.Drawing.Bitmap from the passed WPF BitmapSource instance
 /// </summary>
 /// <param name = "bitmapSource">The bitmap source.</param>
 /// <returns>The generated bitmap</returns>
 public static Bitmap ToBitmap(this BitmapSource bitmapSource)
 {
     var encoder = new BmpBitmapEncoder();
     encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
     using (var stream = new MemoryStream())
     {
         encoder.Save(stream);
         // Nested construction required to prevent issues from closing the underlying stream
         return new Bitmap(stream);
     }
 }
コード例 #28
0
 protected override Image GetFrame(TimeSpan ts)
 {
     var bitmapSource = _mediaDetector.GetImage(ts);
     using (var outStream = new MemoryStream())
     {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create(bitmapSource));
         enc.Save(outStream);
         return new Bitmap(outStream);
     }
 }
コード例 #29
0
 public static Bitmap GetBitmapFromBitmapSource(BitmapSource bSource)
 {
     Bitmap bmp;
       using (MemoryStream ms = new MemoryStream()) {
     BitmapEncoder enc = new BmpBitmapEncoder();
     enc.Frames.Add(BitmapFrame.Create(bSource));
     enc.Save(ms);
     bmp = new Bitmap(ms);
       }
       return bmp;
 }
コード例 #30
0
        private void SaveRTBAsBMP(RenderTargetBitmap bmp, string filename)
        {
            var enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));

            using (var stm = File.Create(filename))
            {
                enc.Save(stm);
            }
        }
コード例 #31
0
 public static System.Drawing.Image AsGdiPlusImage(this System.Windows.Media.ImageSource image)
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
         encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image as System.Windows.Media.Imaging.BitmapSource));
         encoder.Save(memoryStream);
         memoryStream.Flush();
         return(System.Drawing.Image.FromStream(memoryStream));
     }
 }
コード例 #32
0
        public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return new Bitmap(bitmap);
            }
        }
コード例 #33
0
 public System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource)
 {
     Bitmap bitmap;
     using (MemoryStream outStream = new MemoryStream())
     {
         BitmapEncoder encoder = new BmpBitmapEncoder();
         encoder.Frames.Add(BitmapFrame.Create(bitmapsource));
         encoder.Save(outStream);
         bitmap = new System.Drawing.Bitmap(outStream);
     }
     return bitmap;
 }
コード例 #34
0
 public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
 {
     Bitmap bitmap;
     using (var outStream = new MemoryStream())
     {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create(bitmapsource));
         enc.Save(outStream);
         bitmap = new Bitmap(outStream);
     }
     return bitmap;
 }
コード例 #35
0
        /// <summary>
        /// Show image out side passing.
        /// source: https://stackoverflow.com/questions/6484357/converting-bitmapimage-to-bitmap-and-vice-versa
        /// </summary>
        /// <param name="Disp"></param>
        private static void ShowImage(System.Windows.Media.Imaging.BitmapImage Disp)
        {
            using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(Disp));
                enc.Save(outStream);
                SrcImg = new System.Drawing.Bitmap(outStream);

                pbCtrl.Disp(SrcImg);
            }
        }
コード例 #36
0
        private System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srcImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(srcImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return new Bitmap(bitmap);
            }
        }
コード例 #37
0
        public static Bitmap ToWinFormsBitmap(this System.Windows.Media.Imaging.BitmapSource bitmapsource)
        {
            using (MemoryStream stream = new MemoryStream()) {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapsource));
                enc.Save(stream);

                using (Bitmap tempBitmap = new Bitmap(stream)) {
                    return(new Bitmap(tempBitmap));
                }
            }
        }
コード例 #38
0
ファイル: Form1.cs プロジェクト: kfirt/BlindPhone
 private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
 {
     System.Drawing.Bitmap bmp;
     using (MemoryStream outStream = new MemoryStream())
     {
         BitmapEncoder enc = new BmpBitmapEncoder();
         enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
         enc.Save(outStream);
         bmp = new System.Drawing.Bitmap(outStream);
     }
     return bmp;
 }
コード例 #39
0
        private void ShowImage(System.Windows.Media.Imaging.BitmapImage Disp)
        {
            System.Drawing.Bitmap DisplayImg;
            using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(Disp));
                enc.Save(outStream);
                DisplayImg = new System.Drawing.Bitmap(outStream);

                pctbxDisp.Image = DisplayImg;
            }
        }
コード例 #40
0
        private static void SendBitmap(System.Windows.Media.Imaging.BitmapSource b)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type = (byte)ClipboardPacketType.BITMAP;
            packet.name = String.Empty;

            MemoryStream ms = new MemoryStream();

            System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
            System.Windows.Media.Imaging.BitmapImage      bImg    = new System.Windows.Media.Imaging.BitmapImage();
            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(b));
            encoder.Save(ms);
            byte[] getBitmapData = ms.ToArray();

            int maxPayloadLen = ClipboardConstants.MaxPacketPayloadLength;

            packet.payloadLength = maxPayloadLen;
            packet.totalLength   = ms.Length;

            long bytesToSend = ms.Length;

            while (bytesToSend > 0)
            {
                if (bytesToSend < maxPayloadLen)
                {
                    packet.payloadLength = (int)bytesToSend;
                }
                packet.payload = new byte[maxPayloadLen];
                Array.Copy(getBitmapData, ms.Length - bytesToSend, packet.payload, 0, packet.payloadLength);
                bytesToSend -= packet.payloadLength;
                byte[] toSend = Serialization.GetClipboardBytes(packet);
                Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
            }
            ms.Close();
        }
コード例 #41
0
        private static BitmapSource SharpenFixedByCanvas(BitmapSource input, int width, int height)
        {
            if (input.PixelWidth == width && input.PixelHeight == height)
            {
                return(input);
            }

            if (input.Format != PixelFormats.Bgra32 || input.Format != PixelFormats.Pbgra32)
            {
                input = new FormatConvertedBitmap(input, PixelFormats.Bgra32, null, 0);
            }

            //Use the same scale for x and y to keep aspect ratio.
            double scale = Math.Min((double)width / input.PixelWidth, height / (double)input.PixelHeight);

            int x = (int)Math.Round((width - (input.PixelWidth * scale)) / 2);
            int y = (int)Math.Round((height - (input.PixelHeight * scale)) / 2);

            var scaled = new TransformedBitmap(input, new ScaleTransform(scale, scale));
            var stride = scaled.PixelWidth * (scaled.Format.BitsPerPixel / 8);

            #region Make a WhiteCanvas
            List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>();
            colors.Add(System.Windows.Media.Colors.White);

            BitmapPalette palette = new BitmapPalette(colors);
            System.Windows.Media.PixelFormat pf =
                System.Windows.Media.PixelFormats.Indexed1;
            int strideWhite = width / pf.BitsPerPixel;

            byte[] pixels = new byte[height * strideWhite];

            for (int i = 0; i < height * strideWhite; ++i)
            {
                pixels[i] = 0x00;
            }
            BitmapSource whiteimage = BitmapSource.Create(
                width,
                height,
                96,
                96,
                pf,
                palette,
                pixels,
                strideWhite);

            #endregion

            #region ResizeImage + WhiteCanvas
            // Create a render bitmap and push the surface to it
            var target        = new RenderTargetBitmap(width, height, whiteimage.DpiX, whiteimage.DpiY, PixelFormats.Default);
            var targetVisual  = new DrawingVisual();
            var targetContext = targetVisual.RenderOpen();
            //targetContext.DrawImage(scaled, new System.Windows.Rect(0, 0, scaled.Width, scaled.Height));            //ImageDrawing
            targetContext.DrawImage(scaled, new System.Windows.Rect(0, 0, scaled.PixelWidth, scaled.PixelHeight));    //ImageDrawing
            targetContext.Close();
            target.Render(targetVisual);

            // Adding those two render bitmap to the same drawing visual
            var            resultBitmap = new RenderTargetBitmap(width, height, whiteimage.DpiX, whiteimage.DpiY, PixelFormats.Default);
            DrawingVisual  dv           = new DrawingVisual();
            DrawingContext dc           = dv.RenderOpen();
            dc.DrawImage(whiteimage, new System.Windows.Rect(0, 0, width, height));
            int posTx = (whiteimage.PixelWidth - scaled.PixelWidth) / 2;
            int posTy = (whiteimage.PixelHeight - scaled.PixelHeight) / 2;
            dc.DrawImage(target, new System.Windows.Rect(posTx, posTy, width, height));
            dc.Close();
            resultBitmap.Render(dv);
            #endregion

            #region MediaBitmap to DrawingBitmap - Result Image
            MemoryStream ms      = new MemoryStream();
            var          encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(resultBitmap));
            encoder.Save(ms);
            ms.Flush();
            System.Drawing.Image gdiImage = System.Drawing.Image.FromStream(ms);
            #endregion

            return(ToBitmapImage(ApplySharpen((double)width, (System.Drawing.Bitmap)gdiImage)));  //ShapenImage

            //return scaled;
            //return resultBitmap;
        }