I am working in an application that saves images via web service in asp.net but it throws an exception with the following message:
我正在一个应用程序,通过asp.net中的Web服务保存图像,但它引发了以下消息的异常:
Generic Error in GDI+
GDI +中的一般错误
These are the methods:
这些是方法:
public class GuardaImagenes : System.Web.Services.WebService
{
[WebMethod]
public string GuardarImagen(string image,string imageTitle,string ext)
{
string ImageFolderPath = @"C:\Users\Andres\Pictures\ImagenesEvento\";
Image convertedImage = ConvertToImage(image);
try {
convertedImage.Save(ImageFolderPath+imageTitle+ext,ImageFormat.Jpeg);
return "true";
}catch(Exception ex){
return "false";
}
}
public Image ConvertToImage(string image) {
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(image);
using (var ms = new MemoryStream(imageBytes, 0,imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image img = Image.FromStream(ms, true);
return img;
}
}
}
I pass the image in Base64 string and use the ConvertToImage
method to save the image in the specified folder in my computer.
我在Base64字符串中传递图像并使用ConvertToImage方法将图像保存在计算机的指定文件夹中。
The problem is, when it comes when I want to save the image, the program crashes and can't handle it. I've read that it's better to use MemoryStream
, but I can't implement it correctly.
问题是,当我想保存图像时,程序崩溃并无法处理。我已经读过使用MemoryStream更好,但我无法正确实现它。
2 个解决方案
#1
The error occurs because the memory stream is closed. You can refer to this
发生错误是因为内存流已关闭。你可以参考这个
public Image ConvertToImage(string image)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(image);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image img = Image.FromStream(ms, true);
return img;
}
#2
I have found the solution to this problem, what it is only needed is to write all the image bytes using the class
我找到了解决这个问题的方法,只需要使用该类写入所有图像字节
File
This is my final code:
这是我的最终代码:
[WebMethod]
public string SaveEventImage(string ImageByteArray, string ImageTitle)
{
try
{
byte[] bytes = Convert.FromBase64String(ImageByteArray);
File.WriteAllBytes(EVTImagePath + ImageTitle + ".jpg",bytes);
return "true";
}
catch
{
return "false";
}
}
#1
The error occurs because the memory stream is closed. You can refer to this
发生错误是因为内存流已关闭。你可以参考这个
public Image ConvertToImage(string image)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(image);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image img = Image.FromStream(ms, true);
return img;
}
#2
I have found the solution to this problem, what it is only needed is to write all the image bytes using the class
我找到了解决这个问题的方法,只需要使用该类写入所有图像字节
File
This is my final code:
这是我的最终代码:
[WebMethod]
public string SaveEventImage(string ImageByteArray, string ImageTitle)
{
try
{
byte[] bytes = Convert.FromBase64String(ImageByteArray);
File.WriteAllBytes(EVTImagePath + ImageTitle + ".jpg",bytes);
return "true";
}
catch
{
return "false";
}
}