C# 可移植类库 - 使用图像
C# Portable Class Libraries - Using images
我正在编写一个库,它利用网络 API 下载图像。我需要这个库可以在许多平台上使用,所以我选择了一个可移植类库。通常我会使用 System.Drawing
中的 Bitmap
类型将下载的图像存储在内存中,然后传递给方法等。
但是,PCL 无法访问此类,因为它们采用独立于平台的方式。因此,我正在考虑将图像简单地下载到一个 byte[]
数组中,然后由使用该库的应用程序使用。使用字节数组保存下载的图像会不会有任何潜在问题?
我认为使用 byte[]
存储图像不会有任何重大潜在问题。
我能想到的唯一一件事是将图像转换为 byte[]
并返回的潜在开销。
我也想不出比 byte[]
.
更好的方法来存储图像
此外,您可以为包含 byte[]
的数据 class 编写扩展方法,以便您可以在 return Bitmap
中使用它。
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; }
}
我正在编写一个库,它利用网络 API 下载图像。我需要这个库可以在许多平台上使用,所以我选择了一个可移植类库。通常我会使用 System.Drawing
中的 Bitmap
类型将下载的图像存储在内存中,然后传递给方法等。
但是,PCL 无法访问此类,因为它们采用独立于平台的方式。因此,我正在考虑将图像简单地下载到一个 byte[]
数组中,然后由使用该库的应用程序使用。使用字节数组保存下载的图像会不会有任何潜在问题?
我认为使用 byte[]
存储图像不会有任何重大潜在问题。
我能想到的唯一一件事是将图像转换为 byte[]
并返回的潜在开销。
我也想不出比 byte[]
.
此外,您可以为包含 byte[]
的数据 class 编写扩展方法,以便您可以在 return Bitmap
中使用它。
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; }
}