/// <summary>
        /// Convert BitmapImage to Bitmap
        /// </summary>
        /// <param name="input">BitmapImage which be converted</param>
        /// <returns>Bitmap</returns>
        public static System.Drawing.Bitmap Bitmapimage2Bitmap(System.Windows.Media.Imaging.BitmapImage input)
        {
            if (input == null)
            {
                return(null);
            }

            using (System.IO.MemoryStream outmstream = 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(input));
                enc.Save(outmstream);
                System.Drawing.Bitmap retBitmap = new System.Drawing.Bitmap(outmstream);

                if (retBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                {
                    System.Drawing.Imaging.ColorPalette new_pal = retBitmap.Palette;
                    for (int i = 0; i < 256; i++)
                    {
                        new_pal.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i);
                    }
                    retBitmap.Palette = new_pal;
                }

                return(retBitmap);
            }
        }
        public static void SendBitmap(System.Windows.Media.Imaging.BitmapSource b)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type = BITMAP_TYPE;
            MemoryStream ms = new MemoryStream();

            System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(b));
            encoder.Save(ms);

            byte[] getBitmapData = ms.ToArray();
            packet.length      = BUFFER_SIZE;
            packet.totalLength = ms.Length;
            packet.name        = String.Empty;
            long bytesToSend = ms.Length;

            while (bytesToSend > 0)
            {
                if (bytesToSend < BUFFER_SIZE)
                {
                    packet.length = (int)bytesToSend;
                }
                packet.data = new byte[BUFFER_SIZE];
                Array.Copy(getBitmapData, ms.Length - bytesToSend, packet.data, 0, packet.length);
                bytesToSend -= packet.length;
                byte[] toSend = Serialization.GetClipboardBytes(packet);

                Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
            }
            ms.Close();
        }
        private Bitmap ImageWpfToGDI(System.Windows.Media.ImageSource image)
        {
            MemoryStream ms      = 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(ms);
            ms.Flush();
            return(System.Drawing.Image.FromStream(ms) as Bitmap);
        }
Beispiel #4
0
 public static byte[] ToByteArray(this System.Windows.Media.Imaging.BitmapSource bitmapSource)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
         encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
         encoder.Save(ms);
         return(ms.ToArray());
     }
 }
        // Helper function to generate an Image from a BitmapSource
        private static System.Drawing.Image ImageSourceToGDI(System.Windows.Media.Imaging.BitmapSource src)
        {
            var ms      = new MemoryStream();
            var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(src));
            encoder.Save(ms);
            ms.Flush();
            return(System.Drawing.Image.FromStream(ms));
        }
Beispiel #6
0
        private ToolStripMenuItem SetFonts()
        {
            ToolStripMenuItem menuItem = new ToolStripMenuItem("Insert Font");

            List <string> fonts = new List <string>();

            foreach (System.Windows.Media.FontFamily font in System.Windows.Media.Fonts.SystemFontFamilies) //WPF fonts
            {
                fonts.Add(font.FamilyNames.Values.First());
            }
            fonts.Sort();

            foreach (string fontName in fonts)
            {
                Bitmap bmp = null;
                try
                {
                    int size = 20;
                    //bmp = new Bitmap(4 * size, 5 * size / 4);
                    //Graphics g = Graphics.FromImage(bmp);
                    //g.SmoothingMode = SmoothingMode.HighQuality;
                    //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    //g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    //g.DrawString("Basic", new Font(new FontFamily(fontName), size, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, 1, 1);

                    System.Windows.Media.DrawingVisual dv = new System.Windows.Media.DrawingVisual();
                    using (System.Windows.Media.DrawingContext dc = dv.RenderOpen())
                    {
                        dc.DrawRectangle(System.Windows.Media.Brushes.White, null, new System.Windows.Rect(0, 0, 4 * size, 5 * size / 4));
                        dc.DrawText(new System.Windows.Media.FormattedText("Basic", System.Globalization.CultureInfo.InvariantCulture,
                                                                           System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface(fontName), size,
                                                                           System.Windows.Media.Brushes.Black), new System.Windows.Point(1, 1));
                    }
                    System.Windows.Media.Imaging.RenderTargetBitmap rtb = new System.Windows.Media.Imaging.RenderTargetBitmap(4 * size, 5 * size / 4, 96, 96, System.Windows.Media.PixelFormats.Pbgra32);
                    rtb.Render(dv);
                    rtb.Freeze();
                    MemoryStream stream = new MemoryStream();
                    System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
                    encoder.Save(stream);
                    bmp = new Bitmap(stream);
                }
                catch
                {
                }

                ToolStripMenuItem item = new ToolStripMenuItem(fontName, bmp, Insert);
                item.ImageScaling = ToolStripItemImageScaling.None;

                menuItem.DropDownItems.Add(item);
            }

            return(menuItem);
        }
Beispiel #7
0
        private Bitmap BitmapImage2Bitmap(System.Windows.Media.Imaging.BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return(new Bitmap(bitmap));
            }
        }
Beispiel #8
0
        /// <summary>
        /// BitmapSource(BitmapImage) 转换 System.Drawing.Bitmap
        /// </summary>
        /// <param name="bitmapSource">BitmapSource(BitmapImage) - WPF平台</param>
        /// <returns>System.Drawing.Bitmap</returns>
        public static Bitmap BitmapSource2Bitmap(System.Windows.Media.Imaging.BitmapSource bitmapSource)
        {
            using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
            {
                var enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
                enc.Save(outStream);
                Bitmap bitmap = new Bitmap(outStream);

                return(new Bitmap(bitmap));
            }
        }
        //Need System.Windows DLL & PresentationCore DLL & PresentationFramework & System.xaml & System.windows.presentation
        private System.Drawing.Bitmap BitmapSourceToBitmap(System.Windows.Media.Imaging.BitmapSource bitmapsource)
        {
            System.Drawing.Bitmap  bitmap;
            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(bitmapsource));
            enc.Save(outStream);
            bitmap = new System.Drawing.Bitmap(outStream);

            return(bitmap);
        }
Beispiel #10
0
        private Bitmap BitmapImage2Bitmap(System.Windows.Media.Imaging.BitmapImage bitmapImage)
        {
            // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));

            using (MemoryStream outStream = new MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return(new Bitmap(bitmap));
            }
        }
Beispiel #11
0
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            System.Windows.Media.Imaging.BmpBitmapEncoder oBmpBitmapEncoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            byte[] bytes        = null;
            var    bitmapSource = value as System.Windows.Media.Imaging.BitmapSource;

            if (bitmapSource != null)
            {
                oBmpBitmapEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));

                using (var stream = new System.IO.MemoryStream())
                {
                    oBmpBitmapEncoder.Save(stream);
                    bytes = stream.ToArray();
                }
            }

            return(bytes);
        }