Fastest way to draw a bitmap? - c#

I'm working on a timelapse application and that requires drawing new frame every 30ms. Frames are stored in isolated storage (they are 640x480).
I tried loading them into MemoryStream first, and then convert to BitmapImage and assign as a Source for the Image control. But it's too long - it takes about 55ms. I measured and it's not reading from isolated storage, it's actually loading image into Image control that take the longest.
Is there any way to draw images faster on windows phone with silverlight or should I consider doing so with XNA?

Take a look at the WriteableBitmap class and the open source library WritableBitmapEx. The Blit method within WriteableBitmapEx will copy one bitmap into another. Not sure if it's fast enough for what you need, but it is very fast for what I'm doing with it.

Related

Import/Setting background image to windows form cause run slow

I import background image to every forms in my project now my programs runs slow?
How can I fix this?
the format for the image is png.
Any things to consider in to doing this? to solve the issue? thanks!
In your Form,set property DoubleBuffered to true
Buffered graphics require that the updated graphics data is first
written to a buffer. The data in the graphics buffer is then quickly
written to displayed surface memory. The relatively quick switch of
the displayed graphics memory typically reduces the flicker that can
otherwise occur.
Instead of using PNG format use JPEG format with Quality 12(final image quality is like PNG format),your image size decrease and your form load faster.

Draw on PNG without System.Drawing.Graphics

For debugging purposes, I'd like to draw on an image in my c# app (I have lots of tiles and I'd like to know which one is which). However, when trying to do it via System.Drawing.Graphics, I run into the fact that the image I'm trying to draw on is a GIF (which I don't have any choice over). Is there another way to draw on images in c#/.NET without using the classes in System.Drawing?
Technically, this isn't the question you asked, but I'm guessing the reason you're not able to draw on the Bitmap from your GIF is that the pixel format is indexed. If so, that's easy to work around by making a copy with a non-indexed format using the Bitmap.Clone method. Just give it the full bitmap size and PixelFormat.Format32bppArgb.
I don't know any other standard drawing API's in .NET that are convenient for this purpose.

C# fast pixel rendering

I'm developing depth processing (Xbox Kinect, Asus Xtion, etc) applications using OpenNI.
I need a really simple and fast way of drawing on a Windows form when new depth data is available from the sensor (30 or 60 fps depending on resolution).
Currently I'm invalidating a double-buffered panel from a seperate thread when the data becomes available, and then setting pixels of a bitmap in the panel's paint method, yielding a predictably awful 5fps.
System.Drawing.Graphics seems to lack a fast way to set individual pixels, unless anyone can instruct otherwise.
I literally just need to set pixel colours, so would like to avoid using third party high speed rendering APIs if possible, and ideally use something as native as possible.
Does anyone have any suggestions?
If you're using Bitmaps, then you should use LockBits and UnlockBits to access the data directly in memory. In C#, you can get some extra performance by using unsafe code blocks and pointers.
See this link for more information: http://web.archive.org/web/20150227183132/http://bobpowell.net/lockingbits.aspx
image.SetPixel() is very slow when you are replacing many pixels per frame and you need many frames per second.
It will be a lot faster when you use a WriteableBitmap and call CopyPixels
This way you can fill the array with pixel data using multiple threads and simply blit the array to the image in a single call.
EDIT
Note that WriteableBitmap is a WPF class. If you are bound to WinForms you might need to create your own implementation. WPF/WinForms/GDI interop: converting a WriteableBitmap to a System.Drawing.Image?
You could try my LINQ image processing library to work with your "buffer-bitmaps". It uses an accessible LINQ syntax but is very performant for large bitmaps. It is available on Nuget as a single file include in your project.
Hope that helps!

What WPF image type should I use to perform multiple transformations

I need to perform multiple operations on an Image, for example, I need to resize the image, perhaps pad it to maintain aspect ratio (and draw a background color), and conditionally stamp with a watermark.
I'm currently using BitmapFrame as the type that I pass between the methods involved.
Can anyone recommend another type which I should use to perform incremental updates on images?
I could possibly create a composition of various images, although I'm not sure which types I should use for this?
WriteableBitmap is suitable when you want to make incremental updates of the image. Both BitmapFrame and WriteableBitmap inherit from BitmapSource, and WriteableBitmap can be instantiated using any BitmapSource.
You might also want to have a look at the WriteableBitmapEx library that provides a wealth of efficient WriteableBitmap extension methods for bitmap manipulation. This library is applicable to WPF applications as well as Silverlight, WP7 and Metro.
Try WriteableBitmap
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx
Use the WriteableBitmap class to update and render a bitmap on a per-frame basis. This is useful for generating algorithmic content, such as a fractal image, and for data visualization, such as a music visualizer.
For greater control over updates, and for multi-threaded access to the back buffer, use the following workflow.
1.
Call the Lock method to reserve the back buffer for updates.
2.
Obtain a pointer to the back buffer by accessing the BackBuffer property.
3.
Write changes to the back buffer. Other threads may write changes to the back buffer when the WriteableBitmap is locked.
4.
Call the AddDirtyRect method to indicate areas that have changed.
5.
Call the Unlock method to release the back buffer and allow presentation to the screen.
When updates are sent to the rendering thread, the rendering thread copies the changed rectangles from the back buffer to the front buffer. The rendering system controls this exchange to avoid deadlocks and redraw artifacts, such as "tearing".

C# - Draw one or more HBitmaps on a Bitmap without using Image.FromHBitmap

I receive multiple (small) images stored in HBitmaps (handles to GDI bitmaps) and I want to draw them together in a large C# System.Drawing.Bitmap object. For performance reasons, I don't want to use Image.FromHBitmap to first convert the HBitmaps, as the method makes a copy of the GDI bitmap.
Is there a way to do this? Graphics.DrawImage seems to require System.Drawing Image/Bitmap objects.
I'm using C#/Windows Forms/System.Drawing.
The only other way I can think of is to use the old GDI APIs. In other words, create two device contexts, SelectObject the bitmaps into them and BitBlt on to the other.

Categories

Resources