Create a BitmapImage from ushort[] - c#

Is it possible to create a BitmapImage from a ushort array? and if so how?
At the minute I'm creating a Bitmap, converting it to a Bitmap array and displaying it, but this is too slow, I need to continuously update the image (live video feed), while each frame is being created the UI studders, this is making my app very slow when video is running. So I need to get my ushort[] into a BitmapImage as fast as possible
Thanks,
Eamonn

here you have an example of how to get a BitmapImage through a MemoryStream, this might help you
you can use BitConverter to convert ushorts to byte for input to MemoryStream

Assuming you're working with values between 0 and 255 you could cast it into an array of bytes and then load it into a MemoryStream:
// Please note that with values higher than 255 the program will throw an exception
checked
{
ushort[] values = { 200, 100, 30/*, 256*/ };
var bytes = (from value in values
select (byte)value).ToArray();
// Taken from: http://stackoverflow.com/questions/5346727/wpf-convert-memory-stream-to-bitmapimage
using (var stream = new MemoryStream(data))
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
}
}

Related

Convert BitmapImage to byte[] array in Windows Phone 8.1 Runtime

There are a few samples to do this but they are for Windows Phone 8.0 or 8.1 Silverlight.
But how can you do this for Windows Phone 8.1 Runtime?
You cannot extract the pixels from a Windows.UI.Xaml.Media.Imaging.BitmapImage.
The most general solution is to use a WriteableBitmap instead of a BitmapImage. These classes are both BitmapSources and can be used almost interchangeably. The WriteableBitmap provides access to its pixel data via its PixelBuffer property:
byte[] pixelArray = myWriteableBitmap.PixelBuffer.ToArray(); // convert to Array
Stream pixelStream = wb.PixelBuffer.AsStream(); // convert to stream
Otherwise you will need to acquire the pixels from wherever the BitmapImage got them from. Depending on how the BitmapImage was initialized you may be able to find its origin from its UriSource property. The WinRT Xaml Toolkit has an extension method FromBitmapImage to create a WriteableBitmap from an BitmapImage based on its UriSource.
An ugly option would be to render the BitmapImage into an Image, create a RenderTargetBitmap based on the Image and then get its Pixels with RenderTargetBitmap.CopyPixelsAsync()
Tried this?
private byte[] ConvertToBytes(BitmapImage bitmapImage)
{
byte[] data = null;
using (MemoryStream stream = new MemoryStream())
{
WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
data = stream.GetBuffer();
}
return data;
}

Converting a BitmapImage byte array to an Image byte array (WinRT -> WinForms)

I have 2 solutions in this project. A windows forms solution and a Windows 8.1 Tablet project.
This is what's supposed to happen:
User takes a picture using the tablet and uploads it to a MySQL database in the form of a byte array.
User starts up the windows forms application and loads the byte array from the MySQL database.
It then converts the byte array to an image which is placed in a picturebox.
I'm storing the byte array like this:
CameraCaptureUI dialog = new CameraCaptureUI();
dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
Size aspectRatio = new Size(16, 9);
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
BitmapImage bitmapImage = new BitmapImage();
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
{
var readStream = fileStream.AsStreamForRead();
byte[] pixeBuffer = new byte[readStream.Length];
await readStream.ReadAsync(pixeBuffer, 0, pixeBuffer.Length);
}
The byte array is successfully stored in my database.
I'm running into a problem when converting the byte array into a WinForms Image.
This is my code:
using (var ms = new MemoryStream(bytes))
{
Image i = Image.FromStream(ms);
return i;
}
This gives me an invalid parameter exception.
I'm guessing it's something with the image format? I'm really new to streams though so I have no idea.
Any help is welcome!
PS: I know storing in the SQL database runs perfectly since I can store and load images perfectly using the WinForms application only.
Have you tried setting the position of the MemoryStream back to the beginning before trying to create the Image...
ms.Seek(0, SeekOrigin.Begin);
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap
(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
return ms.ToArray();
}
Try it ,
it's working with me

Conversion of a Bitmap to Pixel Array for Microsoft OCR C#

I'm working with Microsoft's OCR library and am having problems converting the BitmapImage to a pixel array.
I'm making this application for Windows Phone 8, and WriteableBitmap.PixelBuffer.ToArray() isn't an option so I have a static function that'll change a normal BitmapImage into a byte array to feed into the OCR engine.
Well, every time I feed it in the application crashes. What's wrong here?
Here is my static class with the bitmap converter
public static class ByteArrayChange
{
public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
{
byte[] data = null;
using (MemoryStream stream = new MemoryStream())
{
WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
data = stream.GetBuffer();
}
return data;
}
}
Here is the piece of code in the OCR method that's causing the application to crash.
byte[] pa = ByteArrayChange.ConvertToBytes(bitmap);
//Here Is he problem
var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, pa);
What am I doing wrong here?
Thanks!
You're saving your image as JPEG, but I'm fairly certain that OCR library accept RGB/BGRA as an input.
So why don't you use Pixels property? It represents image as BGRA array, so the only thing you need is to convert it to byte[] array.

How to change a Android.graphics.bitmap into a byte array in c#

I have an android.graphics.bitmap and an android.net.Uri both of these I can use anyway I want. But I don't know how to take the bitmap and turn it into a byte[] I have tried using a Parcel but I can't initialize it, and when I use it in the writetoparcel method for both the bitmap and uri it throws an error.
I tried the bitmaps ToArray method and that does nothing but create an empty array.
I also tried to use the compress method but I cannot initialize a stream. The text editor throws an error about creating a new Stream inside an abstract class.
Is there some reference that I am missing that allows me to do this.
This is what I ended up using to get it into a byte array and resize.
Bitmap thumb;
Android.Net.Uri val;
this.thumb = MediaStore.Images.Media.GetBitmap(this.ContentResolver, this.val);
Bitmap scaledThumb = Bitmap.CreateScaledBitmap(this.thumb, 1600, 1200, true);
MemoryStream stream = new MemoryStream();
scaledThumb.Compress(Bitmap.CompressFormat.Jpeg, 50, stream);
this.byteArr = stream.ToArray();
This is how you do it.
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
My guess is you would want to use:
Android.Graphics.Bitmap.CopyPixelsToBuffer (Java.Nio.Buffer)
or:
Android.Graphics.Bitmap.GetPixels (int[], int, int, int, int, int, int)

WPF Images not rendering from in-memory sources

I have a data class in my application which maintains a collection of byte arrays representing JPEG images. It's defined as:
private ArrayList FrameList = new ArrayList();
I'm having some troubles with my Image object rendering a blank page (and taking its sweet time to do it as well). When I insert a blank image with a 2K in-memory byte array (byte x[] = { lots of hex values };):
FrameList.Insert(currFrame, x);
and then import a JPEG file over it later on with:
byte[] bytes = File.ReadAllBytes(fspec);
FrameList[currFrame] = bytes;
the array is read correctly into memory and stored in the ArrayList (confirmed with the debugger).
However,I then have a function to get the image:
public BitmapImage getCurrPicture()
{
MemoryStream strm;
BitmapImage bmp = new BitmapImage();
strm = new MemoryStream((byte[])FrameList[currFrame-1]);
bmp.CacheOption = BitmapCacheOption.None;
bmp.BeginInit();
bmp.StreamSource = strm;
bmp.EndInit();
strm.Close();
return bmp;
}
which is called:
imgPicB.Source = data.getCurrPicture();
and it doesn't always render.
imgPicB is defined in my XAML as:
<Image x:Name="imgPicB"
Width="400"
Height="300"
Stretch="Fill"
VerticalAlignment="Top" />
Funny thing is, if I use the exact same JPEG setting the source with setting the source to the file URI directly, it renders fine.
Is there some problem with using in-memory JPEG images in WPF? Is there some extra smarts performed when loading from a file (say auto-detection of the image type)?
Try this:
public BitmapSource GetCurrPicture()
{
var bitmapImage = new BitmapImage();
using (Stream stream = new MemoryStream((byte[])FrameList[currFrame-1]))
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
This works by having WPF decode the image immediately with OnLoad, and then release the reference to the stream when it is done. This means the stream can be garbage collected when it leaves the function. Otherwise, BitmapImage could hold onto the stream until it is rendered.
In the code posted in the question, the render would fail because:
the decode is delayed; and
the stream has been closed at the point where the decode tries to happen.
Link
DwayneNeed 20 Jun 2008 5:11 PM Comments
Caching BitmapImage will store the decoded bitmap in system memory. You can control when this happens by setting the CacheOption property. BitmapImage also maintains a cache of previous BitmapImage instances (via weak references) so that loading the same Uri multiple times will share the same instance. To avoid this cache, you can include the BitmapCreateOptions.IgnoreImageCache flag in the CreateOptions property.

Categories

Resources