Reputation: 6170
I'm writing a library which makes use of a web API to download images. I need this library to be useable on a number of platforms, so I'm opting for a portable class library. Normally I would use the Bitmap
type from System.Drawing
to store the downloaded image in memory to then be passed to a method etc.
However, PCL's don't have access to this class because of their platform independent manner. Therefore, I was thinking of simply downloading the images into a byte[]
array which would then be used by the application using the library. Will there be any potential problems with using a byte array to hold the downloaded images?
Upvotes: 2
Views: 1267
Reputation: 8852
I don't believe there is any major potential problem in using byte[]
to store images.
Only one thing that I can think of is some potential overhead of converting images to byte[]
and back.
Also I can't think of any better way to store the images other then in a byte[]
.
Plus you can write an extension method for the data class that holds byte[]
to return Bitmap
where you can use it.
public static class DataClassExtensions
{
public static BitmapImage GetImage(this DataClass data)
{
if(data == null || data.ImageArray == null) return null;
using (var ms = new System.IO.MemoryStream(data.ImageArray))
{
Bitmap image = (Bitmap)Image.FromStream(ms);
return image;
}
}
}
public class DataClass
{
public byte[] ImageArray { get; set; }
}
Upvotes: 3