bitmapsource

WPF/BackgroundWorker and BitmapSource problem

北城以北 提交于 2019-12-06 03:10:49
问题 I am a beginner with WPF and trying a home project to become familiar with the technology. I have a simple form where the user selects an image file, I then display EXIF data along with a thumbnail of the image. This is working fine but when I select a RAW image file (~9 MB) there can be a slight delay while the thumb loads, so I thought I could use the BackgroundWorker to decode the image and the user can view the EXIF data, then when the image has been decoded it is displayed. The

How to read pixels in four corners of a BitmapSource?

送分小仙女□ 提交于 2019-12-05 17:59:24
I have a .NET BitmapSource object. I would like to read the four pixels in corners of the bitmap, and test whether all of them are darker than white. How can I do that? Edit: I wouldn't mind converting this object to another type with a better API. BitmapSource has a CopyPixels method that can be used to get one or more pixel values. A helper method that gets a single pixel value at a given pixel coordinate may look like shown below. Note that it perhaps has to be extended to support all required pixel formats. public static Color GetPixelColor(BitmapSource bitmap, int x, int y) { Color color;

How do I convert from a Brush (e.g. DrawingBrush) to a BitmapSource?

ⅰ亾dé卋堺 提交于 2019-12-05 07:06:32
I have a DrawingBrush with some vector graphics. I want to convert it to BitmapSource as an intermediate step to getting it to Bitmap. What's the (best) way to do this? public static BitmapSource BitmapSourceFromBrush(Brush drawingBrush, int size = 32, int dpi = 96) { // RenderTargetBitmap = builds a bitmap rendering of a visual var pixelFormat = PixelFormats.Pbgra32; RenderTargetBitmap rtb = new RenderTargetBitmap(size, size, dpi, dpi, pixelFormat); // Drawing visual allows us to compose graphic drawing parts into a visual to render var drawingVisual = new DrawingVisual(); using

C# WPF BitmapSource Memory Leak?

做~自己de王妃 提交于 2019-12-04 14:14:12
问题 I'm developing a BlackJack program which shows a BlackJack Table, cards, etc. The plan is that it'll be playing thousands of hands one after another with an automated strategy. I have a PlayerSeat UserControl which contains an ItemsControl bound to a ObservableCollection. This CardInHand class contains a BitmapSource named CardImage. When the instance is crated it loads the card image from resources using the following code: [System.Runtime.InteropServices.DllImport("gdi32.dll")] public

WPF/BackgroundWorker and BitmapSource problem

十年热恋 提交于 2019-12-04 07:43:08
I am a beginner with WPF and trying a home project to become familiar with the technology. I have a simple form where the user selects an image file, I then display EXIF data along with a thumbnail of the image. This is working fine but when I select a RAW image file (~9 MB) there can be a slight delay while the thumb loads, so I thought I could use the BackgroundWorker to decode the image and the user can view the EXIF data, then when the image has been decoded it is displayed. The BitmapSource object is declared in the BackgroundWorkers DoWork method: worker.DoWork += delegate(object s,

Copying from BitmapSource to WritableBitmap

廉价感情. 提交于 2019-12-03 12:22:10
I am trying to copy a part of a BitmapSource to a WritableBitmap. This is my code so far: var bmp = image.Source as BitmapSource; var row = new WriteableBitmap(bmp.PixelWidth, bottom - top, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette); row.Lock(); bmp.CopyPixels(new Int32Rect(top, 0, bmp.PixelWidth, bottom - top), row.BackBuffer, row.PixelHeight * row.BackBufferStride, row.BackBufferStride); row.AddDirtyRect(new Int32Rect(0, 0, row.PixelWidth, row.PixelHeight)); row.Unlock(); I get "ArgumentException: Value does not fall within the expected range." in the line of CopyPixels . I tried swapping

Get System.Drawing.Bitmap of a WPF Area using VisualBrush

强颜欢笑 提交于 2019-12-02 02:37:58
问题 The point is, that I need to convert to a System.Drawing.Bitmap (.Net Framework 2.0) to get a single frame of an WPF Grid with its content. I read about VisualBrush and DrawingBrush but I cannot imagine how it should work. I can convert any WPF BitmapSource into my System.Drawing.Bitmap successfully. But how to receive the BitmapSource from my Grid? Thanks 回答1: To convert a Visual to BitmapSource you can use RenderTargetBitmap, VisualBrush and DrawingVisual: public BitmapSource

Get System.Drawing.Bitmap of a WPF Area using VisualBrush

戏子无情 提交于 2019-12-01 23:54:04
The point is, that I need to convert to a System.Drawing.Bitmap (.Net Framework 2.0) to get a single frame of an WPF Grid with its content. I read about VisualBrush and DrawingBrush but I cannot imagine how it should work. I can convert any WPF BitmapSource into my System.Drawing.Bitmap successfully. But how to receive the BitmapSource from my Grid? Thanks To convert a Visual to BitmapSource you can use RenderTargetBitmap , VisualBrush and DrawingVisual : public BitmapSource ConvertToBitmapSource(UIElement element) { var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)

BitmapSource from embedded image

匆匆过客 提交于 2019-12-01 18:14:21
问题 My goal is to draw image "someImage.png", which is embedded resource, on WPF window, in overridden OnRender method: protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { base.OnRender(drawingContext); drawingContext.DrawImage(ImageSource, Rect); } I found code to get my image from resources to Stream: public BitmapSource GetSourceForOnRender() { System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream myStream =

InteropBitmap to BitmapImage

我们两清 提交于 2019-12-01 17:49:28
I'm trying to Convert a Bitmap (SystemIcons.Question) to a BitmapImage so I can use it in a WPF Image control. I have the following method to convert it to a BitmapSource , but it returns an InteropBitmapImage , now the problem is how to convert it to a BitmapImage . A direct cast does not seem to work. Does anybody know how to do it? CODE: public BitmapSource ConvertToBitmapSource() { int width = SystemIcons.Question.Width; int height = SystemIcons.Question.Height; object a = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(SystemIcons.Question.ToBitmap().GetHbitmap(), IntPtr.Zero