Asp.net 无法显示HttpHandler[Image],因为它包含错误

Asp.net 无法显示HttpHandler[Image],因为它包含错误,asp.net,visual-studio-2010,image,c#-4.0,httphandler,Asp.net,Visual Studio 2010,Image,C# 4.0,Httphandler,好的,我一直在不停地研究这个问题,并且做了大量的搜索。从数据库中提取图像时,无法显示图像。如果我尝试手动转到处理程序链接,我会收到一条消息说“图像[image]无法显示,因为它包含错误”。我在数据库中有一些以前的旧图像,它首先正确地显示了这些图像。但是现在如果我更新图像,当我试图查看它们时,它会给我这个错误 上传代码 if (fileuploadImage.HasFile) { if (IsValidImage(fileuploadImage))

好的,我一直在不停地研究这个问题,并且做了大量的搜索。从数据库中提取图像时,无法显示图像。如果我尝试手动转到处理程序链接,我会收到一条消息说“图像[image]无法显示,因为它包含错误”。我在数据库中有一些以前的旧图像,它首先正确地显示了这些图像。但是现在如果我更新图像,当我试图查看它们时,它会给我这个错误

上传代码

if (fileuploadImage.HasFile)
        {
            if (IsValidImage(fileuploadImage))
            {
                int length = fileuploadImage.PostedFile.ContentLength;
                byte[] imgbyte = new byte[length];
                HttpPostedFile img = fileuploadImage.PostedFile;
                img.InputStream.Read(imgbyte, 0, length);

                if (mainImage == null)
                {
                    ProfileImage image = new ProfileImage();
                    image.ImageName = txtImageName.Text;
                    image.ImageData = imgbyte;
                    image.ImageType = img.ContentType;
                    image.MainImage = true;
                    image.PersonID = personID;

                    if (image.CreateImage() <= 0)
                    {
                        SetError("There was an error uploading this image.");
                    }
                }
                else
                {
                    mainImage.ImageName = txtImageName.Text;
                    mainImage.ImageType = img.ContentType;
                    mainImage.ImageData = imgbyte;
                    mainImage.MainImage = true;
                    mainImage.PersonID = personID;

                    if (!mainImage.UpdateImage())
                    {
                        SetError("There was an error uploading this image.");
                    }
                }
            }
            else
            {
                SetError("Not a valid image type.");
            }
这就是我如何称呼它“~/ImageHandler.ashx?ImID=“+Parser.GetString(image.ImageID))

我正在使用SQLServer中的数据类型映像来存储这个

编辑:


我还发现,如果我在context.Response.end()周围放置一个try catch,它会出错,说“无法计算代码,因为本机框架…”

我发现了问题。我正在检查实际文件的头,以确保它是有效的。不知何故,这改变了数据,使其变得糟糕。

我发现了我的问题。我正在检查实际文件的头,以确保它是有效的。不知何故,这改变了数据并使其变得糟糕。

也许内存流没有被重置到其原始位置?也许内存流没有被重置到其原始位置?
public class ImageHandler : IHttpHandler
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        int imageid = Parser.GetInt(context.Request.QueryString["ImID"]);
        ProfileImage image = new ProfileImage(Parser.GetInt(imageid));

        context.Response.ContentType = image.ImageType;
        context.Response.Clear();
        context.Response.BinaryWrite(image.ImageData);
        context.Response.End();
    }