使用C#显示图像

发布于 2024-10-14 12:39:48 字数 1613 浏览 1 评论 0原文

我正在使用 mvc2,我想在控制器中使用操作,例如 ShowSmallImage) ,当我输入 www.url.com/ShowSmallImage 时,在浏览器中输出是图像。

我尝试了这样的事情:

public Bitmap CreateThumbnail()
        {
            Image img1 = Image.FromFile(@"C:...\Uploads\Photos\178.jpg");

            int newWidth = 100;
            int newHeight = 100;
            double ratio = 0;

            if (img1.Width > img1.Height)
            {
                ratio = img1.Width / (double)img1.Height;
                newHeight = (int)(newHeight / ratio);
            }
            else
            {
                ratio = img1.Height / (double)img1.Width;
                newWidth = (int)(newWidth / ratio);
            }

            //a holder for the result 
            Bitmap result = new Bitmap(newWidth, newHeight);

            //use a graphics object to draw the resized image into the bitmap 
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality 
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap 
                graphics.DrawImage(img1, 0, 0, result.Width, result.Height);
            }

            return result;
        }

结果我在浏览器中只得到 System.Drawing.Bitmap 。我想我需要设置页面的响应/内容类型,但不知道该怎么做...

谢谢,

I'm using mvc2 and I would like to use action in controller, for example ShowSmallImage) and when I type www.url.com/ShowSmallImage that in browser the output is an image.

I tried something like this:

public Bitmap CreateThumbnail()
        {
            Image img1 = Image.FromFile(@"C:...\Uploads\Photos\178.jpg");

            int newWidth = 100;
            int newHeight = 100;
            double ratio = 0;

            if (img1.Width > img1.Height)
            {
                ratio = img1.Width / (double)img1.Height;
                newHeight = (int)(newHeight / ratio);
            }
            else
            {
                ratio = img1.Height / (double)img1.Width;
                newWidth = (int)(newWidth / ratio);
            }

            //a holder for the result 
            Bitmap result = new Bitmap(newWidth, newHeight);

            //use a graphics object to draw the resized image into the bitmap 
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality 
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap 
                graphics.DrawImage(img1, 0, 0, result.Width, result.Height);
            }

            return result;
        }

As a result I get only System.Drawing.Bitmap in browser. I suppose I need to set response/content type of the page but have no idea how to do it...

Thanks,
Ile

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

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

发布评论

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

评论(2

み青杉依旧 2024-10-21 12:39:48

创建一个 fileresult 并将流返回到位图&设置内容类型:

    private FileResult RenderImage()
    {   
        MemoryStream stream = new MemoryStream();
        var bitmap = CreateThumbnail();
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        Byte[] bytes = stream.ToArray();
        return File(bytes, "image/png");

    }

Create a fileresult and return the stream to the bitmap & set the content type:

    private FileResult RenderImage()
    {   
        MemoryStream stream = new MemoryStream();
        var bitmap = CreateThumbnail();
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        Byte[] bytes = stream.ToArray();
        return File(bytes, "image/png");

    }
﹏雨一样淡蓝的深情 2024-10-21 12:39:48

在控制器中,例如 ResourceController,您可以有一个返回 FileResultAction。像这样

    public FileResult Thumbnail()
    {
        var bitmap = // Your method call which returns a Bitmap

        var ms = new MemoryStream();
        bitmap.Save(ms, ImageFormat.Png);
        return new FileStreamResult(ms, "image/png");
    }

然后你可以调用http://www.mysite.com/Resource/Thumbnail

In a controller, say ResourceController you could have an Action that returns a FileResult. Like so

    public FileResult Thumbnail()
    {
        var bitmap = // Your method call which returns a Bitmap

        var ms = new MemoryStream();
        bitmap.Save(ms, ImageFormat.Png);
        return new FileStreamResult(ms, "image/png");
    }

Then you can call http://www.mysite.com/Resource/Thumbnail.

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