值不能为空。参数名称:位图转换c#中的编码器
Value cannot be null. Parameter name: encoder in bitmap casting c#
为了将位图转换为 base64,我必须将位图转换为图像
当我从像 Image img = (Image)bitmap;
或 Image img = bitmap as Image
这样的屏幕截图位图投射我的图像后将图像转换为内存流时收到此消息 img.Save(ms, img.RawFormat);
,但是当我使用本地存储的图像时它工作正常喜欢 Image img = Image.FromFile(Path)
。
当我不想存储屏幕截图并每次都重新阅读时,如何避免此错误
这是我试过的代码
Image img = bitmap as Image;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
string base64 = Convert.ToBase64String(ms.ToArray());
}
实际上,您不必将 Bitmab
转换为 Image
,而您可以直接将 Bitmab
编码为 Base64
试试这个:
using (MemoryStream ms = new MemoryStream())
{
bitmab.Save(ms, ImageFormat.Jpeg); // you can change your image format as you want
byte[] imageBytes = ms.ToArray();
string base64 = Convert.ToBase64String(imageBytes);
}
为了将位图转换为 base64,我必须将位图转换为图像
当我从像 Image img = (Image)bitmap;
或 Image img = bitmap as Image
这样的屏幕截图位图投射我的图像后将图像转换为内存流时收到此消息 img.Save(ms, img.RawFormat);
,但是当我使用本地存储的图像时它工作正常喜欢 Image img = Image.FromFile(Path)
。
当我不想存储屏幕截图并每次都重新阅读时,如何避免此错误
这是我试过的代码
Image img = bitmap as Image;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
string base64 = Convert.ToBase64String(ms.ToArray());
}
实际上,您不必将 Bitmab
转换为 Image
,而您可以直接将 Bitmab
编码为 Base64
试试这个:
using (MemoryStream ms = new MemoryStream())
{
bitmab.Save(ms, ImageFormat.Jpeg); // you can change your image format as you want
byte[] imageBytes = ms.ToArray();
string base64 = Convert.ToBase64String(imageBytes);
}