简体   繁体   English

如何在 ASP.NET 2.0 中将图像放大到正常大小

[英]How to enlarge image to normal size in ASP.NET 2.0

  • My users uploads a picture and the picture get saved into my SQl Server Database我的用户上传图片,图片保存到我的 SQl 服务器数据库中
  • My users view the picture on the page in a normal Image WebControl with size 150px by 150px - example can be found http://www.erate.co.za/CompanyProfile.aspx?ID=112 - the USA flag is the Image i am talking about.我的用户在大小为 150 像素 x 150 像素的普通图像 WebControl 中查看页面上的图片 - 示例可以在http://www.erate.co.za/CompanyProfile.aspx?ID=112中找到 - 美国标志是图像 i我在谈论。

If the user uploads a picture of 300px by 200px the image will look funny in my static 150px by 150px Image box.如果用户上传一张 300 像素 x 200 像素的图片,该图像在我的 static 150 像素 x 150 像素图像框中会看起来很有趣。 Any idea how i can display the Image so it wont be fuzzy?知道如何显示图像以使其不会模糊吗?

Also, how can a have some form of pop-up that will appear on the screen, showing the actual size of the picture from the database when the user clicks on the 150px by 150px Image.此外,当用户单击 150 像素 x 150 像素的图像时,如何在屏幕上显示某种形式的弹出窗口,显示数据库中图片的实际大小。

Note: I dont want the Image static size to increase as this will totally mess up my page layout and look.注意:我不希望图像 static 大小增加,因为这会完全弄乱我的页面布局和外观。 It must be some form of pop-up or something.它必须是某种形式的弹出窗口或其他东西。 Something i have seen before i think was really cool.我以前看过的东西我觉得真的很酷。 User clicked on the small image and then a pop-up appeared in the middle of the screen with the actual size of the picure and everything ells on the page was disabled until the user clicked on the close button.用户单击小图像,然后在屏幕中间出现一个带有图片实际大小的弹出窗口,页面上的所有内容都被禁用,直到用户单击关闭按钮。 Can this be done in ASP.NET 2.0?这可以在 ASP.NET 2.0 中完成吗? Or something like it?或者类似的东西?

Regards Etienne问候艾蒂安

Use thumbnails of a fixed width/height.使用固定宽度/高度的缩略图。

You resize the actual images (as stored in the database) on the fly.您可以即时调整实际图像(存储在数据库中)的大小。

So your thumnail size is always 150px by 150px.因此,您的缩略图大小始终为 150 像素 x 150 像素。 The actual images (width/height) as stored in the database would vary.存储在数据库中的实际图像(宽度/高度)会有所不同。

You start out by showing the list of resized thumbnails which display the full image in the popup window when you click on the thumbnail item.您首先显示调整大小的缩略图列表,当您单击缩略图项目时,这些缩略图会在弹出窗口 window 中显示完整图像。

Code to 'thumbnail' an image: “缩略图”图像的代码:

        byte[] getResizedImage(String path, int width, int height)
        {
            try
            {
                Bitmap imgIn = new Bitmap(path);

                //Scale
                double y = imgIn.Height;
                double x = imgIn.Width;
                double factor = 1;
                if (width > 0)
                {
                    factor = width / x;
                }
                else if (height > 0)
                {
                    factor = height / y;
                }

                System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));
                Graphics g = Graphics.FromImage(imgOut);
                g.Clear(Color.White);
                g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)), new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);
                imgOut.Save(outStream, ImageFormat.Jpeg);

                return outStream.ToArray();

            }
            catch (Exception)
            {
...
            }

        }

This example uses a.jpg image on the web server file system but can be adjusted to cater for any images/types stored in the db.此示例使用 web 服务器文件系统上的 .jpg 图像,但可以进行调整以适应数据库中存储的任何图像/类型。

Mooi bly.莫伊布莱。

Edit: Fixed ImageFormat编辑:固定图像格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM