问题描述
我想从我的 Asp.Net 网页中的 url 加载图像。问题是,我读取的字节无法在图像中转换,我总是得到一个无效的参数。我尝试了不同的方法,没有任何效果。以下是我的一些代码:
using (WebClient webClient = new WebClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//This is working,the image I want to read is on my local harddrive (before
I downloaded the image from the Uri by hand)
webClient.DownloadData("~/Images/Muster_des_Personalausweises_VS.jpg");
//Here I try to get the same image from the original place,but the byte
array data is not a valid parameter for the following webClient
byte[] data = webClient.DownloadFile(uri,"~/Images/myTestimage.jpg"); //error (the image cannot be shown,as the bytes are invalid)
//This was another try
byte[] data = webClient.DownloadData(uri);
ImageConverter converter = new ImageConverter();
Image img = (Image)converter.ConvertFrom(data); //error invalid parameter
//And another try
Stream stream = webClient.OpenRead(uri);
Bitmap bitmap;
bitmap = new Bitmap(stream); //error invalid parameter
//And another try
Image.FromStream(new MemoryStream(data,data.Length)); //error parameter invalid
//And another try
using (MemoryStream mem = new MemoryStream(data))
{
using (var myImage = Image.FromStream(mem)) //Error parameter invalid
{
If you want it as Png
myImage.Save("path_to_your_file.png",ImageFormat.Png);
If you want it as Jpeg
myImage.Save(uri,ImageFormat.Jpeg);
}
}
}
如您所见,我确实尝试了不同的可能性。问题似乎是 byte[] data = webClient.DownloadData(uri);或者它可能是来自 url 本身的图像?我尝试使用 uri 本身在网页中加载图像,但也无法正常工作:
<td>
@{string base64String = Convert.ToBase64String(Model.TestObject);
<img src="@String.Format("data:image/png;base64,{0}",base64String)" width="300" height="200" />} //showing nothing
@*<img src="@Url.Content(Model.TestObject)" style="width:inherit" />*@ //also not showing anything. Model.TestObejct contains the uri
</td>
或者可能是来源本身。有谁知道问题可能是什么?任何帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)