如何关闭图像流 [英] how to close image stream

查看:148
本文介绍了如何关闭图像流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下代码打开图片:



hi , i open an image whit the below code :

protected void Page_Load(object sender, EventArgs e)
{
    string imagePath = Server.MapPath(PublicClass.ImgPath()) + "3_nissan-cars-logo-emblem.jpg"; // address of my local image
    Image originalImage = Image.FromFile(imagePath);
    System.Drawing.Image image;
    image = ResizeImage(originalImage, 100, 100);
    Response.ContentType = "image/Jpeg";
    image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}







public static System.Drawing.Image ResizeImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
    int newWidth = 0, newHeight = 0;


    if ((image.Width > maxWidth || image.Height > maxHeight) && maxWidth > 0 && maxHeight > 0)
    {
        newWidth = maxWidth;
        newHeight = image.Height * newWidth / image.Width;


        var newImage = new Bitmap(maxWidth, newHeight);
        using (var graphic = Graphics.FromImage(newImage))
        {
            graphic.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }
    else
        if (maxWidth > 0 && maxHeight > 0)
        {
            var newImage = new Bitmap(image.Width, image.Height);
            using (var graphic = Graphics.FromImage(newImage))
            {
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
            }
            return newImage;
        }
        else
            return image;
}





我想从我的目录中删除图像,当它打开时我收到有关图像正在运行的错误在另一个处理器



请帮助我如何显示图像whit我的上述代码并丢弃其流可以删除它。谢谢。



and i would like to delete image from my directory while its open and i get error regarding "image is running in another proccessor"

please help me that how to show image whit my above code and discard its stream to can delete it . thank you .

推荐答案

一个简单的答案是每个使用代码的流和资源都会公开这个方法 .Close()通过使用哪个,您可以轻松关闭资源,以便下一个进程可以轻松使用它。无需向您抛出异常。



几天前我已经发布了一个答案,其中包含有关关闭流以及如何使用.NET的信息C#中的代码可以在您完成工作后自动关闭流。



如何关闭文件流 [ ^ ]
A simple answer is that every stream and resource using code exposes this method .Close() by using which, you can easily close the resource so that a next process can easily use it. Without having to throw an exception to you.

I have already posted an answer a few days ago, which had information about closing the streams and how to use the .NET code in C# efficiently to close the streams automatically once you're done working with them.

How to close file stream[^]


这篇关于如何关闭图像流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆