How to convert BitmapImage into byte array in silverlight4? - c#

I am trying to convert the BitmapImage into byte array, but not getting the exact solution.
The Image.Save() method is not available in Silverlight Library.
How to fix this?

if you image source is in the project you can use this..
var uri = new Uri("/ReferencedAssembly;component/default.png", UriKind.Relative);
var streamInfo = System.Windows.Application.GetResourceStream(uri);
var stream = streamInfo.Stream;
convert the stream to bytes array..
if image is one some other location
Using httpwebrequest to get image from website to byte[]
Regards

Related

Byte Array to Image using C# in Xamarin

I've been searching for the past few days but did not manage to find a solution for my problem. I am currently working on a xamarin Android app. I want to display an image by using the byte array column from by database. I am using another program to find the byte array of a specific photo and after that I insert manually its value in the byte array column from my principal project.
This is my code where I am trying to reproduce the image:
Android.Graphics.Bitmap bitmap=BitmapFactory.DecodeByteArray(currentexercis.image, 0, currentexercis.image.Length);
viewHolder.exercis_photo.SetImageBitmap(bitmap);
Currentexercis.image represents the byte array from my database, and its value seems to be OK, however every time bitmap is null.
This is the code from my other program where I convert the image into bytearray:
Image img = Image.FromFile(opendlg.FileName);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
dbpicEntities1 db = new dbpicEntities1();
db.MyPictures.Add(new MyPicture() { FileName=fileName, Data = ms.ToArray() });
db.SaveChanges();
MessageBox.Show("success");
I think you should use like this.
byte [] imageArray // is your data
MemoryStream mStream = new MemorySteram ();
mStream.write(imageArray,0,imageArray.Length);
Image img = Image.FromStream(mStream);
img.save(filelocation);
Bitmap bitmapimg = BitmapFactory.BitmapFactory.DecodeStream(mStream);
// if you want to use Bitmap
Byte Array to Image using C# in Xamarin
There are some third-party library which implements this feature quite well like Picasso or Glide.
For Glide, there is official document shows how to use it in Xamarin.Android project: Binding a .JAR.
Or you could directly use it from the nuget package:
Then you can code for example like this:
Glide.With(context)
.Load(imageBytes)
.Apply(RequestOptions.CircleCropTransform())
.Into(imageView);
convert to byteArray
byte[] imgdata = System.IO.File.ReadAllBytes(pathToImage);
convert byte array to bitmap
private void OnGetMemberAvatarCompleted(byte[] avatarBytes)
{
var avatarImageView = FindViewById<ImageView>(Resource.Id.memberProfile_avatar);
if (avatarImageView != null)
{
var imageBitmap = BitmapFactory.DecodeByteArray(avatarBytes, 0,avatarBytes.Length);
RunOnUiThread(() => avatarImageView.SetImageBitmap(imageBitmap));
}
}

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.

Getting a Byte Array from the audio file saved by Media.RecordSoundAction

I have a file that is created using
var recordIntent = new Intent(MediaStore.Audio.Media.RecordSoundAction);
I have no problems retrieving the URI of this file. And I can play it back using MediaPlayer without any difficulties.
However, I would like to send this as a response to my webAPI, and am looking at a way to convert the Audio File represented by this URI to a byte array that I can convert to JSON.
With an image file i can do something like
Bitmap bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, responseUri);
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
Is there a similar way I can retrieve the Byte Array data from my audio URI?
edit:
formatting of Audio URI.
responseUri = {content://media/external/audio/media/21}
Taken from a ton of different SO answers, and a little bit of extra conversion for mono from Java I came up with these results.
public String GetRealPathFromUri(Uri contentUri){
String[] proj = {MediaStore.Audio.AudioColumns.Data};
ICursor cursor = ManagedQuery(contentUri, proj, null, null, null);
int column_index = cursor.GetColumnIndex(MediaStore.Audio.AudioColumns.Data);
cursor.MoveToFirst();
return cursor.GetString(column_index);
}
var responseRealPath = GetRealPathFromUri(responseUri);
var getBytes = System.IO.File.ReadAllBytes(responseRealPath);
var responseBase = Convert.ToBase64String(getBytes);

convert image to byte[] c# and get image back in android

I have the following code in c# which convert image to the byte[]
MemoryStream memory = new MemoryStream();
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Jpeg);
now i want to get the image back in the android, what should i do ?
Im not really sure what your problem is as you haven't answered anyones question but if you just want to make a bitmap out of a byte array this how to do it
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
Convert ByteArray to Base64 string in C#. Then Convert Base64 string to ByteArray in android an do everything you want. In this method you can work with any object type in addition to bitmap.

Load a byte[] into an Image at Runtime

I have a byte[] that is represented by an Image. I am downloading this Image via a WebClient. When the WebClient has downloaded the picture and I reference it using its URL, I get a byte[]. My question is, how do I load a byte[] into an Image element in WPF? Thank you.
Note: This is complementary to the question I asked here: Generate Image at Runtime. I cannot seem to get that approach to work, so I am trying a different approach.
Create a BitmapImage from the MemoryStream as below:
MemoryStream byteStream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = byteStream;
image.EndInit();
And in XAML you can create an Image control and set the above image as the Source property.
You can use a BitmapImage, and sets its StreamSource to a stream containing the binary data. If you want to make a stream from a byte[], use a MemoryStream:
MemoryStream stream = new MemoryStream(bytes);
In .Net framework 4.0
using System.Drawing;
using System.Web;
private Image GetImageFile(HttpPostedFileBase postedFile)
{
if (postedFile == null) return null;
return Image.FromStream(postedFile.InputStream);
}
One way that I figured out how to do it so that it was both fast and thread safe was the following:
var imgBytes = value as byte[];
if (imgBytes == null)
return null;
using (var stream = new MemoryStream(imgBytes))
return BitmapFrame.Create(stream,BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
I threw that into a converter for my WPF application after running the images as Varbinary from the DB.

Categories

Resources