开发者

what is the easiest way to convert an array of bytes into a WPF bitmap

Given an array of bytes, width, height,开发者_Go百科 and a number of bits per pixel, what is the easiest way to create a WPF bitmap.


You can create a WriteableBitmap as you know the width, height and BPP (which will map to PixelFormat). You can then write the bytes to the WriteableBitmap with WritePixels.


If your array is already in a format listed by PixelFormats, you may directly use BitmapSource.Create().


There are a few parameters you should keep in mind when creating a BitmapSource from scratch in WPF. In this case it looks like you are looking for a solution to a very specific image. In your situation I normally use a helper class that encapsulate all these parameters and provide a simple approach to modify the pixels in a matrix way in case you need it. Finally, the way I use to obtain the bitmap is this:

BitmapSource.Create(Width, Height, DpiX, DpiY, PixelFormat, null, PixelData, Stride);

You can take a look at the entire class here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜