Esempio n. 1
0
        static public BitmapSource GetBitmapSource(Bitmap bitmap)
        {
            BitmapSource resultBitmapSource = null;
            IntPtr       ip = IntPtr.Zero;

            try
            {
                if (bitmap == null)
                {
                    return(null);
                }
                ip = bitmap.GetHbitmap();
                resultBitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
                                                                                                  IntPtr.Zero, System.Windows.Int32Rect.Empty,
                                                                                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                resultBitmapSource?.Freeze();
                bitmap.Dispose();
            }
            catch (Exception e)
            {
            }
            finally
            {
                DeleteObject(ip);
            }
            return(resultBitmapSource);
        }
Esempio n. 2
0
        private void FrameUpdatedCallback()
        {
            ImageAnimator.UpdateFrames();

            BitmapSource?.Freeze();
            // Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
            GetBitmapSource(this, Bitmap);
            Source = BitmapSource;
            InvalidateVisual();
        }
Esempio n. 3
0
 private void FrameUpdatedCallback()
 {
     ImageAnimator.UpdateFrames();
     _source?.Freeze();
     _source = GetSource();
     if (_source != null)
     {
         Image.Source = _source;
     }
     Image.InvalidateVisual();
 }
Esempio n. 4
0
    public static BitmapSource GetImageStream(Image myImage)
    {
        var bitmap = new Bitmap(myImage);

        IntPtr       bmpPt        = bitmap.GetHbitmap();
        BitmapSource bitmapSource =
            System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bmpPt,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

        //freeze bitmapSource and clear memory to avoid memory leaks
        bitmapSource.Freeze();
        DeleteObject(bmpPt);

        return(bitmapSource);
    }
Esempio n. 5
0
        public static BitmapSource GetBitMapSource(Icon icon)
        {
            if (icon == null)
            {
                return(null);
            }

            Bitmap bitmap  = icon.ToBitmap();
            IntPtr hBitmap = bitmap.GetHbitmap();

            BitmapSource wpfBitmap =
                Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

            wpfBitmap.Freeze();
            return(wpfBitmap);
        }
Esempio n. 6
0
        internal BitmapSource GetBitmap(FotografiaDto fotografiaDto)
        {
            byte [] bytes = SSClientSingleton.Instance.getImage(fotografiaDto.id);

            MemoryStream memoryStream = new MemoryStream(bytes);
            BitmapImage  bi           = new BitmapImage();

            memoryStream.Position = 0;
            bi.BeginInit();
            bi.StreamSource = memoryStream;
            bi.EndInit();
            BitmapSource bitmapSource = bi;

            // Se non frizzo, non riesco a passare queste bitmap da un thread all'altro.
            bitmapSource.Freeze();

            return(bitmapSource);
        }
Esempio n. 7
0
        public bool GetImage(out BitmapSource bmpSource, out System.Drawing.Bitmap bmp)
        {
            bool result = false;

            bmp       = null;
            bmpSource = null;

            try
            {
                if (Monitor.TryEnter(imageLock, 5000))
                {
                    try
                    {
                        bmp = new System.Drawing.Bitmap(latestBitmap.Width, latestBitmap.Height,
                                                        latestBitmap.PixelFormat);
                        using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp))
                        {
                            gr.DrawImage(latestBitmap,
                                         new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));
                        }


                        bmpSource = latestBitmapSrc.Clone();
                        bmpSource.Freeze();

                        result = true;
                    }
                    finally
                    {
                        Monitor.Exit(imageLock);
                    }
                }
                else
                {
                    throw new Exception("timed out waiting for imageLock");
                }
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                result    = false;
            }
            return(result);
        }
        private void ThreadWriteVideo(object state)
        {
            using (ColorFrame colorFrame = ((ColorFrameArrivedEventArgs)state).FrameReference.AcquireFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }

                FrameDescription colorFrameDescription = colorFrame.FrameDescription;

                using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
                {
                    m_ColorBitmap.Lock();
                    if (colorFrameDescription.Width == m_ColorBitmap.PixelWidth &&
                        colorFrameDescription.Height == m_ColorBitmap.PixelHeight)
                    {
                        //ui stuff
                        var size = (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4);
                        colorFrame.CopyConvertedFrameDataToIntPtr(m_ColorBitmap.BackBuffer, size, ColorImageFormat.Bgra);
                        m_ColorBitmap.AddDirtyRect(new Int32Rect(0, 0, m_ColorBitmap.PixelWidth,
                                                                 m_ColorBitmap.PixelHeight));

                        BitmapSource source = BitmapSource.Create(m_ColorBitmap.PixelWidth, m_ColorBitmap.PixelHeight,
                                                                  m_ColorBitmap.DpiX, m_ColorBitmap.DpiY, m_ColorBitmap.Format, m_ColorBitmap.Palette,
                                                                  m_ColorBitmap.BackBuffer, (int)size, m_ColorBitmap.BackBufferStride);

                        using (var stream = new MemoryStream())
                        {
                            source.Freeze();
                            BitmapEncoder bitmapEncoder = new BmpBitmapEncoder();
                            bitmapEncoder.Frames.Add(BitmapFrame.Create(source));

                            bitmapEncoder.Save(stream);
                            var bitmap = new Bitmap(stream);

                            m_Writer.WriteVideoFrame(bitmap);
                            m_Recording = true;
                        }
                    }
                    m_ColorBitmap.Unlock();
                }
            }
        }
Esempio n. 9
0
        public async Task <BitmapSource> FetchImage(string URLlink)
        {
            try
            {
                BitmapSource bitmapSource = null;

                await Task.Run(() => {
                    var temp     = new JpegBitmapDecoder(new Uri(URLlink, UriKind.Absolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                    bitmapSource = temp.Frames[0];
                    bitmapSource.Freeze();
                });

                return(bitmapSource);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 10
0
        public static BitmapSource IconCreate(this string filepath, int iconindex)
        {
            if (filepath != null)
            {
                string defaultIcon = filepath;
                IntPtr hIcon       = hwnd.ExtractIcon(defaultIcon, iconindex);
                if (hIcon != IntPtr.Zero)
                {
                    BitmapSource icon = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    hIcon.DestroyIcon();
                    icon.Freeze();
                    return(icon);
                }

                hIcon.DestroyIcon();
            }

            return(null);
        }
Esempio n. 11
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var meta = (GrdMetaData)info;

            stream.Position = 12;
            using (var reader = new Reader(stream.AsStream, meta.DataSize))
            {
                reader.Unpack();
                byte[] pixels = reader.Data;
                using (var bmp = new MemoryStream(reader.Data))
                {
                    var decoder = new BmpBitmapDecoder(bmp,
                                                       BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    BitmapSource frame = decoder.Frames[0];
                    frame.Freeze();
                    return(new ImageData(frame, info));
                }
            }
        }
Esempio n. 12
0
        private void OnFrameChangedInMainThread()
        {
            ImageAnimator.UpdateFrames();

            if (m_bitmapSource != null)
            {
                m_bitmapSource.Freeze();
            }

            m_bitmapSource = GetBitmapSource();
            if (null == m_bitmapSource)
            {
                return;
            }

            Source = m_bitmapSource;

            InvalidateVisual();
        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Image myImage = (Image)value;

            var          bitmap       = new Bitmap(myImage);
            IntPtr       bmpPt        = bitmap.GetHbitmap();
            BitmapSource bitmapSource =
                System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    bmpPt,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

            //freeze bitmapSource and clear memory to avoid memory leaks
            bitmapSource.Freeze();
            DeleteObject(bmpPt);

            return(bitmapSource);
        }
Esempio n. 14
0
        public override DocumentPage GetPage(int page_zero_based)
        {
            last_document_page?.Dispose();
            last_document_page = null;

            int page = page_from + page_zero_based;

            StatusManager.Instance.UpdateStatus("PDFPrinter", String.Format("Printing page {0} of {1}", page_zero_based + 1, PageCount), page_zero_based + 1, PageCount, true);

            // Render a page at 300 DPI...
            using (MemoryStream ms = new MemoryStream(pdf_document.GetPageByDPIAsImage(page, 300)))
            {
                using (Image image = Image.FromStream(ms))
                {
                    PDFOverlayRenderer.RenderAnnotations(image, pdf_document, page, null);
                    PDFOverlayRenderer.RenderHighlights(image, pdf_document, page);
                    PDFOverlayRenderer.RenderInks(image, pdf_document, page);
                    BitmapSource image_page = BitmapImageTools.CreateBitmapSourceFromImage(image);
                    image_page.Freeze();

                    DrawingVisual dv = new DrawingVisual();
                    using (DrawingContext dc = dv.RenderOpen())
                    {
                        // Rotate the image if its orientation does not match the printer
                        if (
                            page_size.Width < page_size.Height && image_page.Width > image_page.Height ||
                            page_size.Width > page_size.Height && image_page.Width < image_page.Height
                            )
                        {
                            image_page = new TransformedBitmap(image_page, new RotateTransform(90));
                            image_page.Freeze();
                        }

                        dc.DrawImage(image_page, new Rect(0, 0, page_size.Width, page_size.Height));
                    }

                    ++total_pages_printed;

                    last_document_page = new DocumentPage(dv);
                    return(last_document_page);
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Loads a bitmap into a bitmap source
        /// </summary>
        private static BitmapSource LoadBitmap(Bitmap source)
        {
            IntPtr       ip = source.GetHbitmap();
            BitmapSource bs = null;

            try
            {
                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
                                                                                  IntPtr.Zero, Int32Rect.Empty,
                                                                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                bs.Freeze();
            }
            finally
            {
                DeleteObject(ip);
            }

            return(bs);
        }
Esempio n. 16
0
        public static BitmapSource ToBitmapSource(this Bitmap bitmap, bool isFrozen = false)
        {
            BitmapSource res = null;

            if (bitmap != null)
            {
                res = Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                if (isFrozen)
                {
                    res.Freeze();
                }
            }

            return(res);
        }
Esempio n. 17
0
 /// <summary>
 /// Set Image.Source.
 /// </summary>
 /// <param name="image"></param>
 /// <param name="fileName"></param>
 private static void SetImageSource(System.Windows.Controls.Image image, string fileName)
 {
     if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
     {
         System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(fileName);
         int imageWidth = 0, imageHeight = 0;
         InitializeImageSize(sourceImage, image, out imageWidth, out imageHeight);
         // From: http://stackoverflow.com/questions/3923697/is-it-possible-to-modify-a-wpf-bitmapsource-in-memory-unsafely-from-another-thr
         Bitmap       sourceBmp    = new Bitmap(sourceImage, imageWidth, imageHeight);
         IntPtr       hBitmap      = sourceBmp.GetHbitmap();
         BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
                                                                           BitmapSizeOptions.FromEmptyOptions());
         bitmapSource.Freeze();
         WriteableBitmap writeableBmp = new WriteableBitmap(bitmapSource);
         sourceImage.Dispose();
         sourceBmp.Dispose();
         image.Source = writeableBmp;
     }
 }
        public void RunCommand(IPEndPoint ipEndPoint, byte[] command)
        {
            switch (command[0])
            {
            case 1:
                break;

            case 4:
                String strListIPPort = System.Text.Encoding.UTF8.GetString(command, 1, command.Length - 1);
                listIPPort = IPPort.GetListIPPort(strListIPPort);
                break;

            case 7:

                break;

            case 8:
                IPPort ipport = listIPPort.Where(ipp => { return(ipp.UserId == command[1]); }).FirstOrDefault();
                String mess   = System.Text.Encoding.UTF8.GetString(command, 3, command.Length - 3);

                chatWindowManager.ShowChatWindow(ipport, mess);
                break;

            case 9:
                String strListUser = System.Text.Encoding.UTF8.GetString(command, 1, command.Length - 1);
                listUser = User.GetListUser(strListUser);
                listViewUsers.Dispatcher.Invoke(() =>
                {
                    listViewUsers.ItemsSource = listUser;
                });
                break;

            case 10:
                int x = BitConverter.ToInt32(command.Skip(3).Take(4).ToArray(), 0);
                int y = BitConverter.ToInt32(command.Skip(7).Take(4).ToArray(), 0);

                BitmapSource screenImage = BitmapSourceFromArray(command.Skip(11).ToArray());
                screenImage.Freeze();

                viewScreenManager.SetImage(command[1], screenImage, x, y);
                break;
            }
        }
Esempio n. 19
0
        private object method_5(object object_1)
        {
            object       obj2   = null;
            BitmapSource source = null;

            if (object_1 is Icon)
            {
                Icon icon = object_1 as Icon;
                using (MemoryStream stream = new MemoryStream())
                {
                    icon.Save(stream);
                    stream.Seek(0L, SeekOrigin.Begin);
                    source = BitmapFrame.Create(stream);
                    goto Label_0079;
                }
            }
            if (object_1 is Bitmap)
            {
                IntPtr hbitmap = (object_1 as Bitmap).GetHbitmap();
                source = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                source.Freeze();
                DeleteObject(hbitmap);
            }
Label_0079:
            if (source != null)
            {
                if (base.TargetPropertyType == typeof(object))
                {
                    return(new System.Windows.Controls.Image {
                        Source = source, Width = source.Width, Height = source.Height
                    });
                }
                return(source);
            }
            obj2 = object_1;
            Type targetPropertyType = base.TargetPropertyType;

            if (((targetPropertyType != null) && (object_1 is string)) && ((targetPropertyType != typeof(string)) && (targetPropertyType != typeof(object))))
            {
                obj2 = TypeDescriptor.GetConverter(targetPropertyType).ConvertFromInvariantString(object_1 as string);
            }
            return(obj2);
        }
Esempio n. 20
0
        private Task StartPreview()
        {
            App.LogWriter.WriteLog("Start Preview");
            CancellationToken token = tokenSource.Token;

            isPreview = true;
            return(Task.Run(async() => {
                while (token.IsCancellationRequested == false)
                {
                    try
                    {
                        await Task.Delay(TimeSpan.FromSeconds(previewFrameTime));

                        Watcher watcher = null;
                        WatcherList.Dispatcher.Invoke(() => { watcher = WatcherList.SelectedItem as Watcher; });

                        if (watcher == null)
                        {
                            continue;
                        }

                        BitmapSource bi = watcher.WebBitmapImage;
                        if (bi != null)
                        {
                            bi.Freeze();
                            Preview.Dispatcher.Invoke(() => { Preview.Source = bi; });
                            bi = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        App.LogWriter.WriteLog($"Preview Error: {ex.Message}");
                        continue;
                    }
                }
                isPreview = false;
                tokenSource = new CancellationTokenSource();
                RadioUniform.Dispatcher.Invoke(() => { RadioUniform.IsChecked = true; });
                Preview.Dispatcher.Invoke(() => { Preview.Source = null; });
                App.LogWriter.WriteLog("End Preview");
            }, token));
        }
Esempio n. 21
0
        //Icon (e.g. exe)
        private void PollIconCallback(object state)
        {
            thumbnailInfo input = state as thumbnailInfo;

            WriteableBitmap writeBitmap = input.bitmap;
            IconSize        size        = input.iconsize;

            Bitmap origBitmap  = KeyToBitmap(input.key, size);
            Bitmap inputBitmap = origBitmap;

            System.Drawing.Size outputSize = input.outputSize;
            if (size == IconSize.jumbo || size == IconSize.thumbnail)
            {
                inputBitmap = resizeJumbo(origBitmap, outputSize, 5);
            }
            else
            {
                inputBitmap = resizeImage(origBitmap, outputSize, 0);
            }

            BitmapSource inputBitmapSource = loadBitmap(inputBitmap);

            if (inputBitmapSource.CanFreeze)
            {
                inputBitmapSource.Freeze();
            }
            else if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
            origBitmap.Dispose();
            inputBitmap.Dispose();

            //Update back to source.
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                                                     new EventHandler(delegate
            {
                iconDic[input.key] = inputBitmapSource;
            }));

            copyBitmap(inputBitmapSource, writeBitmap, true, 0, false);
        }
Esempio n. 22
0
        public static BitmapSource ToBitmapSource(this Bitmap source)
        {
            var          orgSource = source;
            BitmapSource bs        = null;

            try
            {
                var data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
                                           ImageLockMode.ReadOnly, source.PixelFormat);

                // BitmapSource.Create throws an exception when the image is scanned backward.
                // The Clone() will make it back scanning forward.
                if (data.Stride < 0)
                {
                    source.UnlockBits(data);
                    source = (Bitmap)source.Clone();
                    data   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
                                             ImageLockMode.ReadOnly, source.PixelFormat);
                }

                bs = BitmapSource.Create(source.Width, source.Height, Math.Floor(source.HorizontalResolution),
                                         Math.Floor(source.VerticalResolution), ConvertPixelFormat(source.PixelFormat), null,
                                         data.Scan0, data.Stride * source.Height, data.Stride);

                source.UnlockBits(data);

                bs.Freeze();
            }
            catch
            {
                // ignored
            }
            finally
            {
                if (orgSource != source)
                {
                    source.Dispose();
                }
            }

            return(bs);
        }
Esempio n. 23
0
        /// <summary>
        /// Converts a System.Drawing.Bitmap to a System.Windows.Media.Imaging.BitmapSource
        /// </summary>
        public static BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
        {
            BitmapSource bitmapSource = null;

            if (bitmap != null)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();
                try
                {
                    bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                        hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    bitmapSource.Freeze();
                }
                finally
                {
                    DeleteObject(hBitmap);
                }
            }
            return(bitmapSource);
        }
Esempio n. 24
0
        public static BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitmap)
        {
            IntPtr       hBitmap = bitmap.GetHbitmap();
            BitmapSource img     = null;

            try
            {
                img = Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    System.Windows.Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                img.Freeze();
            }
            finally
            {
                DeleteObject(hBitmap);
            }
            return(img);
        }
Esempio n. 25
0
        private void OnTimer(object o)
        {
            System.Drawing.Bitmap image = (System.Drawing.Bitmap)_device.Screenshot.ToImage();

            IntPtr intPtr = image.GetHbitmap();

            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                intPtr,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            bitmapSource.Freeze();
            DeleteObject(intPtr);

            _shotImg.Dispatcher.Invoke(() =>
            {
                _shotImg.Source = bitmapSource;
            });
        }
Esempio n. 26
0
        internal static void Get(ref BitmapSource icon, string source, SIIGBF flags)
        {
            try
            {
                IShellItem ppsi = null;

                SafeNativeMethods.SHCreateItemFromParsingName(source, IntPtr.Zero, guidItemUuid, out ppsi);

                IntPtr hbitmap = IntPtr.Zero;
                ((IShellItemImageFactory)ppsi).GetImage(new SIZE(48, 48), flags | SIIGBF.BIGGERSIZEOK, out hbitmap);
                icon = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                SafeNativeMethods.DeleteObject(hbitmap);

                if (icon != null)
                {
                    icon.Freeze();
                }
            }
            catch { }
        }
Esempio n. 27
0
        /// <summary>
        /// Alls the passes of window.
        /// </summary>
        /// <param name="src">The source.</param>
        /// <param name="step">The step.</param>
        /// <returns></returns>
        public Task <List <RecognizedObject>[]> AllPassesOfWindow(BitmapSource src, int step)
        {
            var preprocessedArray = _imageProcessor.PreprocessImage(src, Filter.Sobel);
            var preprocessedImage = AuxiliaryMethods.BitmapSourceFromArray(preprocessedArray, false);

            int width  = Constants.WIDTH;
            int height = Constants.HEIGHT;

            var taskList = new List <Task <List <RecognizedObject> > >();

            while (width < preprocessedImage.Width && height < preprocessedImage.Height)
            {
                src.Freeze();
                taskList.Add(OnePassOfWindow(src, width, height, step));
                width  = (int)Math.Round(width * 1.5);
                height = (int)Math.Round(height * 1.5);
            }

            return(Task.WhenAll(taskList));
        }
        /// <summary>
        /// Converts between System Drawing bitmaps to WPF BitmapSources
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static BitmapSource SystemDrawingBitmapToBitmapSource(Bitmap bitmap)
        {
            // allocate the memory for the bitmap
            IntPtr bmpPt = bitmap.GetHbitmap();

            // create the bitmapSource
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bmpPt,
                IntPtr.Zero,
                System.Windows.Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            // freeze the bitmap to avoid hooking events to the bitmap
            bitmapSource.Freeze();

            // free memory
            SystemUtils.DeleteObject(bmpPt);

            return(bitmapSource);
        }
Esempio n. 29
0
 /// <summary>
 /// 从一个Bitmap创建ImageSource
 /// </summary>
 /// <param name="image">Bitmap对象</param>
 /// <returns></returns>
 public static ImageSource CreateImageSourceFromImage(Bitmap image)
 {
     if (image == null)
     {
         return(null);
     }
     try
     {
         IntPtr       ptr = image.GetHbitmap();
         BitmapSource bs  = Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty,
                                                                  BitmapSizeOptions.FromEmptyOptions());
         bs.Freeze();
         image.Dispose();
         System.Utility.Win32.Win32.DeleteObject(ptr);
         return(bs);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Esempio n. 30
0
        //TODO: get rid of this port requirement...
        private void UpdateFilteredImagesSources(int x, int y, int listenPort)
        {
            var img = new byte[width * height];

            img[x + (y * width)] = 255;

            BitmapSource filtered = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, img, width);

            filtered.Freeze();
            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                if (listenPort == 5106)
                {
                    Kinect1FilteredImage.Source = filtered;
                }
                else
                {
                    Kinect2FilteredImage.Source = filtered;
                }
            });
        }