C# Base64 字符串转 JPEG 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18827081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Base64 String to JPEG Image
提问by Subby
I am trying to convert a Base64String to an image which needs to be saved locally.
我正在尝试将 Base64String 转换为需要在本地保存的图像。
At the moment, my code is able to save the image but when I open the saved image, it says "Invalid Image".
目前,我的代码能够保存图像,但是当我打开保存的图像时,它显示“无效图像”。
Code:
代码:
try
{
using (var imageFile = new StreamWriter(filePath))
{
imageFile.Write(resizeImage.Content);
imageFile.Close();
}
}
The Content
is a string
object which contains the Base64 String.
的Content
是一个string
其中包含的Base64字符串对象。
采纳答案by Paul Farry
So with the code you have provided.
因此,使用您提供的代码。
var bytes = Convert.FromBase64String(resizeImage.Content);
using (var imageFile = new FileStream(filePath, FileMode.Create))
{
imageFile.Write(bytes ,0, bytes.Length);
imageFile.Flush();
}
回答by Monah
First, convert the base 64 string to an Image
, then use the Image.Save
method.
首先,将 base 64 字符串转换为Image
,然后使用该Image.Save
方法。
To convert from base 64 string to Image
:
要将 base 64 字符串转换为Image
:
public Image Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
To convert from Image
to base 64 string:
转换Image
为 base 64 字符串:
public string ImageToBase64(Image image,System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to base 64 string
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Finally, you can easily to call Image.Save(filePath);
to save the image.
最后,您可以轻松调用Image.Save(filePath);
以保存图像。
回答by Hussain Ahmed Shamsi
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
回答by Rajesh-Systematix
Front :
正面 :
<Image Name="camImage"/>
Back:
后退:
public async void Base64ToImage(string base64String)
{
// read stream
var bytes = Convert.FromBase64String(base64String);
var image = bytes.AsBuffer().AsStream().AsRandomAccessStream();
// decode image
var decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0);
// create bitmap
var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
await output.SetSourceAsync(image);
camImage.Source = output;
}