转换图像到WP8的base64 [英] convert image into base64 in wp8
本文介绍了转换图像到WP8的base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的手机的画廊拍摄的图像,如下图所示:
私人无效StackPanel_Tap_1(对象发件人,System.Windows.Input.GestureEventArgs E)
{
PhotoChooserTask PCT =新PhotoChooserTask();
pct.Show();
pct.Completed + = pct_Completed;
}无效pct_Completed(对象发件人,PhotoResult E)
{
BitmapImage的IMG =新的BitmapImage(); 如果(e.ChosenPhoto!= NULL)
{
img.SetSource(e.ChosenPhoto);
imgphotochoser.Source = IMG;
}
}
现在我要救这个图像中的数据库中,通过Web服务。所以,我需要这个图像转换成一个base64字符串,但我怎么能这样做呢?
我已经试过这一点,但它抛出一个异常:
公共字符串imagetobase64(图像的图像,
system.drawing.imaging.imageformat格式)
{
使用(MemoryStream的毫秒=新的MemoryStream())
{
//图像转换为byte []
image.save(MS,格式);
字节[] = imagebytes ms.toarray(); //转换字节[]为base64字符串
字符串base64string = convert.tobase64string(imagebytes);
返回base64string;
}
}
解决方案
简单地转换了字节[]
来一个base64 字符串
:
字节[]字节组= NULL;使用(MemoryStream的毫秒=新的MemoryStream())
{
如果(imgphotochoser.Source!= NULL)
{
WriteableBitmap的wbitmp =新的WriteableBitmap的((BitmapImage的)imgphotochoser.Source); wbitmp.SaveJpeg(MS,46,38,0,100);
ByteArray的= ms.ToArray();
}
}
字符串str = Convert.ToBase64String(ByteArray的);
Base64编码为字节[]
:
字节[] = fileBytes Convert.FromBase64String(S);使用(MemoryStream的毫秒=新的MemoryStream(fileBytes,0,fileBytes.Length))
{
ms.Write(fileBytes,0,fileBytes.Length);
的BitmapImage BitmapImage的=新的BitmapImage();
bitmapImage.SetSource(毫秒);
返回BitmapImage的;
}
I have an image taken from my phone gallery, like below:
private void StackPanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
PhotoChooserTask pct = new PhotoChooserTask();
pct.Show();
pct.Completed += pct_Completed;
}
void pct_Completed(object sender, PhotoResult e)
{
BitmapImage img = new BitmapImage();
if (e.ChosenPhoto != null)
{
img.SetSource(e.ChosenPhoto);
imgphotochoser.Source = img;
}
}
Now I want to save this image in a database, via a web service. So, I'm required to convert this image into a base64 string, but how can I do this?
I've tried this, but it throws an exception:
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 base64 string
string base64string = convert.tobase64string(imagebytes);
return base64string;
}
}
解决方案
Simply convert the byte[]
to a base64 string
:
byte[] bytearray = null;
using (MemoryStream ms = new MemoryStream())
{
if (imgphotochoser.Source != null)
{
WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)imgphotochoser.Source);
wbitmp.SaveJpeg(ms, 46, 38, 0, 100);
bytearray = ms.ToArray();
}
}
string str = Convert.ToBase64String(bytearray);
Base64 to byte[]
:
byte[] fileBytes = Convert.FromBase64String(s);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
这篇关于转换图像到WP8的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文