本文介绍了base64转换为图像在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用MVC#。当我尝试转换为Base64的字符串作为图像时,图像是没有得到打开。我从JavaScript越来越的base64字符串,并coverting,为图像控制器。这里是我的code。
VAR的base64 = \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABG4AAAH0CAYAAABsCGvoAAAgAElEQVR4Xuy9C5RV1Zmo++96P3ZVURSv4hEEFdQobYKtJESjJME+6WgTpPUQIrfpY8NRui/aKlcvY2hON0fN5TIUbh/6XDs9bgah0+1BY+gk2p2QFpIAgk9AiGBhlQgIokgVRQFVu3bd9S+c21WbXVV71157Pfb+loNh1ar5+Oc3Z/H46p9zRnqsR3ggAAEIQAACEIAABCAAAQhAAAIQgAAEAkcggrgJ3JwQEAQgAAEIQAACEIAABCAAAQhAAAIQsAkgblgIEIAABCAAAQhAAAIQgAAEIAABCEAgoAQQNwGdGMKCAAQgAAEIQAACEIAABCAAAQhAAAKIG9YABCAAAQhAAAIQgAAEIAABCEAAAhAIKAHETUAnhrAgAAEIQAACEIAABCAAAQhAAAIQgADihjUAAQhAAAIQgAAEIAABCEAAAhCAAAQCSgBxE9CJISwIQAACEIAABCAAAQhAAAIQgAAEIIC4YQ1AAAIQgAAEIAABCEAAAhCAAAQgAIGAEkDcBHRiCAsCEIAABCAAAQhAAAIQgAAEIAABCCBuWAMQgAAEIAABCEAAAhCAAAQgAAEIQCCgBBA3AZ0YwoIABCAAAQhAAAIQgAAEIAABCEAAAogb1gAEIAABCEAAAhCAAAQgAAEIQAACEAgoAcRNQCeGsCAAAQhAAAIQgAAEIAABCEAAAhCAAOKGNQABCEAAAhCAAAQgAAEIQAACEIAABAJKAHET0IkhLAhAAAIQgAAEIAABCEAAAhCAAAQggLhhDUAAAhCAAAQgAAEIQAACEIAABCAAgYASQNwEdGIICwIQgAAEIAABCEAAAhCAAAQgAAEIIG5YAxCAAAQgAAEIQAACEIAABCAAAQhAIKAEEDcBnRjCggAEIAABCEAAAhCAAAQgAAEIQAACiBvWAAQgAAEIQAACEIAABCAAA...AgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBourS9wLLy8vhypUrob+/Pxw8eBALAQIECBAgQIAAAQIECBAonIDgpnAlS7vBY2Nj4e7du6GpqSkMDg4KXNIut94RIECAAAECBAgQIECAQBkBwY0hUjcC09PT4enTp9kMmdIxOzsbhoeHw/z8fOjp6QlDQ0Ohubk5XL16NbS3t4epqakwMjIS2trafvTavXv3ws2bN1e+Pjo6GmZmZkJLS8ua13R0dNSNh4YQIECAAAECBAgQIECAAAHBjTFQNwLj4+Ph2LFjq86y+XzZU29vb4ghzPHjx0NfX1/W/vj11V6LAc+FCxdCDHG6urqy82NANDk5GS5evPija+oGQ0MIECBAgAABAgQIECBAgMAnAcGNYVA3AqsFN4uLi9mSqbm5uZXlUzG4KQUypRkyMbj50ms3btzIlmCVjiNHjmTBzQ+vqRsMDSFAgAABAgQIECBAgAABAoIbY6CeBCYmJrKlUgMDAyvNiq/FmTJHjx4Nly9fDpcuXQp5gpvPZ9yUbr5a2FNPHtpCgAABAgQIECBAgAABAgTMuDEG6kqgtDlxbNTp06fD4cOHw7Vr17J9aU6dOhWWlpbCmTNnvnrGTdwDp7THTWnj49UCoLrC0BgCBAgQIECAAAECBAgQaHgBwU3DDwEABAgQIECAAAECBAgQIECAQL0KCG7qtTLaRYAAAQIECBAgQIAAAQIECDS8wP8AN7hUDozwDE4AAAAASUVORK5CYII=\";字节[]海峡= System.Text.Encoding.Uni code.GetBytes的(Base64);
VAR缓冲= Convert.FromBase64String(Convert.ToBase64String(STR));
var文件=使用Server.Mappath(〜/内容/图片/ err.png);
System.IO.File.WriteAllBytes(文件,缓冲液);
图片IMG =新的图像();
img.ImageUrl =使用Server.Mappath(〜/内容/图片/ err.png);
解决方案
我复制的字符串的base64提示符窗口。我用AJAX方法从发布客户端中的Base64字符串,它为我的作品
C#
[HttpPost]
公共无效Base64ToImage(字符串源)
{
串的base64 = source.Substring(source.IndexOf(,)+ 1);
的base64 = base64.Trim('\\ 0');
字节[] = chartData Convert.FromBase64String的(Base64);
}
AJAX方法
VAR照片= jqplotToImg($('#ChartContent'));
//提示(,照片); $阿贾克斯({类型:POST,
网址:'../Report/Base64ToImage',
异步:假的,
数据:{来源:图片},
成功:功能(数据){
//警报(数据);
}
});
I am using MVC#. When i try To convert to base64 string to image, the image is not getting opened. I am getting base64 string from javascript and coverting that to image in controller. Here's my code.
解决方案
I copied that base64 string prompt window. I used ajax method for posting the base64 string from client and it works for me
C#
[HttpPost]
public void Base64ToImage(string source)
{
string base64 = source.Substring(source.IndexOf(',') + 1);
base64 = base64.Trim('\0');
byte[] chartData = Convert.FromBase64String(base64);
}
AJAX Method
var picture = jqplotToImg($('#ChartContent'));
//prompt("", picture);
$.ajax({ type: 'POST',
url: '../Report/Base64ToImage',
async: false,
data: { source: picture },
success: function (data) {
//alert(data);
}
});
这篇关于base64转换为图像在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!