bitmapsource

InteropBitmap to BitmapImage

▼魔方 西西 提交于 2019-12-01 17:19:31
问题 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

How to save a WPF BitmapSource image to a file?

亡梦爱人 提交于 2019-11-30 02:53:22
In WPF, the System.Windows.Clipboard.getImage() function returns a BitmapSource object. As a newbie in WPF coming from a WinForms background, its not clear to me how to save this image to a file. What are the steps I must take? You need to use an encoder (subclass of BitmapEncoder ). For instance, to save it to the PNG format, you do something like that : public static void SaveClipboardImageToFile(string filePath) { var image = Clipboard.GetImage(); using (var fileStream = new FileStream(filePath, FileMode.Create)) { BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add

Saving 8-bit indexed data in a PNG alters original bytes when using BitmapSource, PngBitmapEncoder/Decoder

雨燕双飞 提交于 2019-11-29 07:45:16
I have a problem when saving and loading a PNG using BitmapSource and PngBitmapEncoder/Decoder. Basically, I'd like to be able to save an image that originated as an array of bytes, and when the PNG is loaded into my program, reload the exact same bytes. Original data preservation is important. At the same time, I'd like the PNG to use a custom palette (an indexed array of 256 colors). I'm trying to save my 8-bit data with a custom indexed palette. The original data can range from 0-255. The palette may be a "Thresholded" palette (e.g. 0-20 is color #1, 21-50 is color #2, etc). What I'm

C# WPF BitmapSource Memory Leak?

纵然是瞬间 提交于 2019-11-29 02:30:10
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 static extern bool DeleteObject(IntPtr hObject); private BitmapSource GenerateCardImage() { Stream

How to save a WPF BitmapSource image to a file?

痴心易碎 提交于 2019-11-28 23:46:39
问题 In WPF, the System.Windows.Clipboard.getImage() function returns a BitmapSource object. As a newbie in WPF coming from a WinForms background, its not clear to me how to save this image to a file. What are the steps I must take? 回答1: You need to use an encoder (subclass of BitmapEncoder). For instance, to save it to the PNG format, you do something like that : public static void SaveClipboardImageToFile(string filePath) { var image = Clipboard.GetImage(); using (var fileStream = new FileStream

Image loading memory leak with C#

时光总嘲笑我的痴心妄想 提交于 2019-11-28 09:16:56
I have a memory leak issue in my application which loads a large amount of images. I'm rather new to C#, and thought my days of memory leak issues were past. I can't figure out the problem - maybe I'm using some unmanaged modules which I'm not handle correctly? To illustrate my problem I've simplified the core of what causes the problem and moved this to a clean project. Note that this is all silly code which doesn't reflect the original application it came from. In the test application I have 2 buttons, triggering two events. Button 1 - Create: Setting an object to the datacontext. This will

Saving 8-bit indexed data in a PNG alters original bytes when using BitmapSource, PngBitmapEncoder/Decoder

怎甘沉沦 提交于 2019-11-28 01:31:23
问题 I have a problem when saving and loading a PNG using BitmapSource and PngBitmapEncoder/Decoder. Basically, I'd like to be able to save an image that originated as an array of bytes, and when the PNG is loaded into my program, reload the exact same bytes. Original data preservation is important. At the same time, I'd like the PNG to use a custom palette (an indexed array of 256 colors). I'm trying to save my 8-bit data with a custom indexed palette. The original data can range from 0-255. The

Why does BitmapSource.Create throw an ArgumentException?

亡梦爱人 提交于 2019-11-27 14:11:51
I'm trying to get an bitmap created from raw data to show in WPF, by using an Image and a BitmapSource: Int32[] data = new Int32[RenderHeight * RenderWidth]; for (Int32 i = 0; i < RenderHeight; i++) { for (Int32 j = 0; j < RenderWidth; j++) { Int32 index = j + (i * RenderHeight); if (i + j % 2 == 0) data[index] = 0xFF0000; else data[index] = 0x00FF00; } } BitmapSource source = BitmapSource.Create(RenderWidth, RenderHeight, 96.0, 96.0, PixelFormats.Bgr32, null, data, 0); RenderImage.Source = source; However the call to BitmapSource.Create throws an ArgumentException, saying "Value does not fall

BitmapSource to BitmapImage

别来无恙 提交于 2019-11-27 13:44:50
问题 I need to parse the content of Clipboard.GetImage() (a BitmapSource ) to a BitmapImage . Does anyone knows how can this be done? 回答1: I've found a clean solution that works: BitmapSource bitmapSource = Clipboard.GetImage(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); MemoryStream memoryStream = new MemoryStream(); BitmapImage bImg = new BitmapImage(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(memoryStream); memoryStream.Position = 0; bImg.BeginInit(); bImg

How do you make sure WPF releases large BitmapSource from Memory?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 04:32:31
System: Windows XP SP3, .NET 3.5, 4GB RAM, Dual 1.6gHz I have a WPF application that loads and transitions (using Storyboard animations) extremely large PNGs. These PNGs are 8190x1080 in resolution. As the application runs it appears to cache the images and the system Memory slowly creeps up. Eventually it chokes the system and throws the OutOfMemoryException. Here are the steps I am currently taking to try to solve this: 1)I am removing the BitmapSource objects from the app 2)I am setting the BitmapSource BitmapCacheOption to None when I load the BitmapSource 3)I am Freezing the BitmapSource