GDI 中发生一般错误;

发布于 2024-12-24 17:57:31 字数 2150 浏览 1 评论 0原文

A 有一个图像上传功能,可以在 localhost 上正常工作,但是当我尝试在 Windows Server 2003 下运行时,我收到错误消息“

This is the code..”,

现在在有人攻击我之前;)我已经查看了之前的答案,并且我已检查所有权限,它们似乎是正确的..文件夹/路径存在,等等。

ImageService imageService = new ImageService();

if (fileBase != null && fileBase.ContentLength > 0 && fileBase.ContentLength <= 2097152 && fileBase.ContentType.Contains("image/"))
{
    var uploadedPath = "~/Assets/Images/";

    Path.GetExtension(fileBase.ContentType);
    var extension = Path.GetExtension(fileBase.FileName);

    if (extension.ToLower() != ".jpg" && extension.ToLower() != ".gif") // only allow these types
    {
        photoViewModel.ImageValid = "Not Valid";
        ModelState.AddModelError("Photo", "Wrong Image Type");
        return View(photoViewModel);
    }

    EncoderParameters encodingParameters = new EncoderParameters(1);
    encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    ImageCodecInfo jpgEncoder = imageService.GetEncoderInfo("image/jpeg");
    var uploadedimage = Image.FromStream(fileBase.InputStream, true, true);

    Bitmap originalImage = new Bitmap(uploadedimage);
    Bitmap newImage = new Bitmap(originalImage, 274, 354);

    Graphics g = Graphics.FromImage(newImage);
    g.InterpolationMode = InterpolationMode.HighQualityBilinear;
    g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);

    var streamLarge = new MemoryStream();
    newImage.Save(streamLarge, jpgEncoder, encodingParameters);

    var fileExtension = Path.GetExtension(extension);
    string newname;
    if (photoViewModel.photoURL != null)
    { newname = photoViewModel.photoURL; }
    else
    { newname = Guid.NewGuid() + fileExtension; }

    var ImageName = newname;
    newImage.Save(Server.MapPath(uploadedPath) + ImageName);
    System.IO.File.WriteAllBytes(Server.MapPath(uploadedPath) + ImageName, streamLarge.ToArray());

    photoViewModel.uploadedPath = uploadedPath;
    photoViewModel.photoURL = ImageName;

    originalImage.Dispose();
    newImage.Dispose();
    streamLarge.Dispose();
    return View(photoViewModel);
}

A have an image upload function that works fine on localhost but when I try and run under Windows Server 2003 I get the error message

This is the code..

Now before anyone jumps on me ;) I've looked at the previous answers and I've checked all permissions and they seem to be correct.. the folder/paths exist, etc..

ImageService imageService = new ImageService();

if (fileBase != null && fileBase.ContentLength > 0 && fileBase.ContentLength <= 2097152 && fileBase.ContentType.Contains("image/"))
{
    var uploadedPath = "~/Assets/Images/";

    Path.GetExtension(fileBase.ContentType);
    var extension = Path.GetExtension(fileBase.FileName);

    if (extension.ToLower() != ".jpg" && extension.ToLower() != ".gif") // only allow these types
    {
        photoViewModel.ImageValid = "Not Valid";
        ModelState.AddModelError("Photo", "Wrong Image Type");
        return View(photoViewModel);
    }

    EncoderParameters encodingParameters = new EncoderParameters(1);
    encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    ImageCodecInfo jpgEncoder = imageService.GetEncoderInfo("image/jpeg");
    var uploadedimage = Image.FromStream(fileBase.InputStream, true, true);

    Bitmap originalImage = new Bitmap(uploadedimage);
    Bitmap newImage = new Bitmap(originalImage, 274, 354);

    Graphics g = Graphics.FromImage(newImage);
    g.InterpolationMode = InterpolationMode.HighQualityBilinear;
    g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);

    var streamLarge = new MemoryStream();
    newImage.Save(streamLarge, jpgEncoder, encodingParameters);

    var fileExtension = Path.GetExtension(extension);
    string newname;
    if (photoViewModel.photoURL != null)
    { newname = photoViewModel.photoURL; }
    else
    { newname = Guid.NewGuid() + fileExtension; }

    var ImageName = newname;
    newImage.Save(Server.MapPath(uploadedPath) + ImageName);
    System.IO.File.WriteAllBytes(Server.MapPath(uploadedPath) + ImageName, streamLarge.ToArray());

    photoViewModel.uploadedPath = uploadedPath;
    photoViewModel.photoURL = ImageName;

    originalImage.Dispose();
    newImage.Dispose();
    streamLarge.Dispose();
    return View(photoViewModel);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-12-31 17:57:31

使用Image.FromStream,流必须在图像的生命周期内保持打开状态。这意味着如果流是一个文件,该文件将保持打开状态。我认为您需要处置 uploadedImage 以允许关闭流(如果您等待 GC 清理 uploadedImage,这将在未来的不确定点发生 - 或者可能根本不会发生)。

With Image.FromStream, the stream must remain open for the lifetime of the image. That means if the stream is a file, the file will be held open. I think you'll need to dispose uploadedImage to allow the stream to be closed (if you wait for the GC to clean up the uploadedImage, this will occur at an indeterminate point in the future - or may not occur at all).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文