缩略图,剪切调整大小并上传到数据库

发布于 2024-09-15 10:35:05 字数 3446 浏览 3 评论 0原文

我使用此代码创建缩略图,然后将原始缩略图和缩略图存储到数据库中。它创建的 tn 始终具有固定大小,如果原始图像比其更高,则将其剪切,然后将其大小调整为固定大小。

该代码正在工作,但是我真的很感谢一些帮助修改此代码以执行以下操作(我已经尝试过但没有成功):

  • 制作高质量缩略图

  • 如果图像太高,则降低高度
    比它的宽度(如果宽度是
    200px 高度为 1000px 会怎样 会发生吗?)

  • 接受 png 和 tiff。

这是到目前为止的代码:

    public void imgUpload()
    {
        if (ImgUpload.PostedFile != null)
        {
            System.Drawing.Image image_file = System.Drawing.Image.FromStream(ImgUpload.PostedFile.InputStream);
            string fileName = Server.HtmlEncode(ImgUpload.FileName);
            string extension = System.IO.Path.GetExtension(fileName);

            bool sizeError = false;

            if(image_file.Width < 200)
                sizeError = true;

            if(image_file.Height < 250)
                sizeError = true;

            if ((extension.ToUpper() == ".JPG") && !sizeError)
            {

                //**** Resize image section ****  
                int image_height = image_file.Height;
                int image_width = image_file.Width;
                int original_width = image_width;
                int original_height = image_height;
                int max_height = 250;
                int max_width = 200;

                Rectangle rect;

                if (image_width > image_height)
                {
                    image_width = (image_width * max_height) / image_height;
                    image_height = max_height;
                    rect = new Rectangle(((image_width - max_width) / 2), 0, max_width, max_height);
                }
                else
                {
                    image_height = (image_height * max_width) / image_width;
                    image_width = max_width;
                    rect = new Rectangle(0, ((image_height - max_height) / 2), max_width, max_height);
                }

                Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                Bitmap new_bitmap_file = bitmap_file.Clone(rect, bitmap_file.PixelFormat);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();

                new_bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Position = 0;

                byte[] imageThumbnail = new byte[stream.Length + 1];
                stream.Read(imageThumbnail, 0, imageThumbnail.Length);

                Bitmap Original_bitmap_file = new Bitmap(image_file, original_width, original_height);
                System.IO.MemoryStream Original_stream = new System.IO.MemoryStream();

                Original_bitmap_file.Save(Original_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                Original_stream.Position = 0;

                byte[] imageOriginal = new byte[Original_stream.Length + 1];
                Original_stream.Read(imageOriginal, 0, imageOriginal.Length);
                //**** End resize image section ****  

                saveImage(imageThumbnail, imageOriginal, IDTextBox.Text);
                lblOutput.Visible = false;
            }
            else
            {
                lblOutput.Text = "Please only upload .jpg files and make sure the size is minimum 200x250";
                lblOutput.Visible = true;
            }
        }
        else
        {
            lblOutput.Text = "No file selected";
            lblOutput.Visible = true;
        }
    }

I use this code to create thumbnails and then store the original and the thumbnail into a DB. It creates tn that are always of a fixed sized and if the original image is wither than it's higher it is cut and then resized to the fixed size.

The code is working however I would really appreciate some help modifying this code to do the following (I have tried it but didn't succeeded):

  • Make high-quality thumbnails

  • cut the height if the image is way taller
    than it's width (If the width is
    200px and height is 1000px what will
    happen?)

  • Accept png and tiff.

This is the code so far:

    public void imgUpload()
    {
        if (ImgUpload.PostedFile != null)
        {
            System.Drawing.Image image_file = System.Drawing.Image.FromStream(ImgUpload.PostedFile.InputStream);
            string fileName = Server.HtmlEncode(ImgUpload.FileName);
            string extension = System.IO.Path.GetExtension(fileName);

            bool sizeError = false;

            if(image_file.Width < 200)
                sizeError = true;

            if(image_file.Height < 250)
                sizeError = true;

            if ((extension.ToUpper() == ".JPG") && !sizeError)
            {

                //**** Resize image section ****  
                int image_height = image_file.Height;
                int image_width = image_file.Width;
                int original_width = image_width;
                int original_height = image_height;
                int max_height = 250;
                int max_width = 200;

                Rectangle rect;

                if (image_width > image_height)
                {
                    image_width = (image_width * max_height) / image_height;
                    image_height = max_height;
                    rect = new Rectangle(((image_width - max_width) / 2), 0, max_width, max_height);
                }
                else
                {
                    image_height = (image_height * max_width) / image_width;
                    image_width = max_width;
                    rect = new Rectangle(0, ((image_height - max_height) / 2), max_width, max_height);
                }

                Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                Bitmap new_bitmap_file = bitmap_file.Clone(rect, bitmap_file.PixelFormat);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();

                new_bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Position = 0;

                byte[] imageThumbnail = new byte[stream.Length + 1];
                stream.Read(imageThumbnail, 0, imageThumbnail.Length);

                Bitmap Original_bitmap_file = new Bitmap(image_file, original_width, original_height);
                System.IO.MemoryStream Original_stream = new System.IO.MemoryStream();

                Original_bitmap_file.Save(Original_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                Original_stream.Position = 0;

                byte[] imageOriginal = new byte[Original_stream.Length + 1];
                Original_stream.Read(imageOriginal, 0, imageOriginal.Length);
                //**** End resize image section ****  

                saveImage(imageThumbnail, imageOriginal, IDTextBox.Text);
                lblOutput.Visible = false;
            }
            else
            {
                lblOutput.Text = "Please only upload .jpg files and make sure the size is minimum 200x250";
                lblOutput.Visible = true;
            }
        }
        else
        {
            lblOutput.Text = "No file selected";
            lblOutput.Visible = true;
        }
    }

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

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

发布评论

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

评论(2

月下客 2024-09-22 10:35:05

这是如何缩放和裁剪图像的示例:

Bitmap b = new Bitmap(200, 1000);
using (var g = Graphics.FromImage(b))
{
    g.DrawLine(Pens.White, 0, 0, b.Width, b.Height);
}
b.Save("b.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Bitmap thumb = new Bitmap(100, 100);
using (var g = Graphics.FromImage(thumb))
{
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.DrawImage(b, new Rectangle(0,0,100,100), new Rectangle(0, 400, 200, 200), GraphicsUnit.Pixel);
}
thumb.Save("thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

我认为您可以调整代码以适应高度/宽度比率过高时的所有情况和情况等。

Here is an example of how to scale and crop an image:

Bitmap b = new Bitmap(200, 1000);
using (var g = Graphics.FromImage(b))
{
    g.DrawLine(Pens.White, 0, 0, b.Width, b.Height);
}
b.Save("b.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Bitmap thumb = new Bitmap(100, 100);
using (var g = Graphics.FromImage(thumb))
{
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.DrawImage(b, new Rectangle(0,0,100,100), new Rectangle(0, 400, 200, 200), GraphicsUnit.Pixel);
}
thumb.Save("thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

I think you can adapt the code to all what's and if's about when the ratio height/width is to high, etc.

输什么也不输骨气 2024-09-22 10:35:05

我在为 http://www.inventerat.se 和其他一些网站创建缩略图时使用以下代码它的工作原理就像一个魅力:

    public static Bitmap WithinMaxSize(Image imgPhoto, int maxWidth, int maxHeight)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        int destWidth;
        int destHeight;

        float sourceAspect = sourceWidth / (sourceHeight * 1.0F);
        float maxAspect = maxWidth / (maxHeight * 1.0F);
        if (sourceAspect > maxAspect)
        {
            // Width is limiting.
            destWidth = maxWidth;
            destHeight = (int)Math.Round(destWidth / sourceAspect);
        }
        else
        {
            // Height is limiting.
            destHeight = maxHeight;
            destWidth = (int)Math.Round(destHeight * sourceAspect);
        }

        Bitmap bmPhoto = new Bitmap(destWidth, destHeight);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.CompositingMode = CompositingMode.SourceCopy;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto, 0, 0, destWidth, destHeight);

        grPhoto.Dispose();
        return bmPhoto;
    }

只是不要忘记处理从此方法返回的位图。

但如果用户上传非常大的图像(例如> 5000 * 5000 像素),它就不能很好地工作。如果这是一个要求,我建议使用 ImageMagick 或类似的成像包。

要接受 png 和 tiff,只需调整检查文件扩展名的方式,以便也接受“png”、“tif”和“tiff”。 .NET 中对 tiff 的支持有点粗略,因为 tiff 本身只是许多不同编码的容器。

I use the following code when creating thumbnails for http://www.inventerat.se and a few other sites and it works like a charm:

    public static Bitmap WithinMaxSize(Image imgPhoto, int maxWidth, int maxHeight)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        int destWidth;
        int destHeight;

        float sourceAspect = sourceWidth / (sourceHeight * 1.0F);
        float maxAspect = maxWidth / (maxHeight * 1.0F);
        if (sourceAspect > maxAspect)
        {
            // Width is limiting.
            destWidth = maxWidth;
            destHeight = (int)Math.Round(destWidth / sourceAspect);
        }
        else
        {
            // Height is limiting.
            destHeight = maxHeight;
            destWidth = (int)Math.Round(destHeight * sourceAspect);
        }

        Bitmap bmPhoto = new Bitmap(destWidth, destHeight);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.CompositingMode = CompositingMode.SourceCopy;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto, 0, 0, destWidth, destHeight);

        grPhoto.Dispose();
        return bmPhoto;
    }

Just don't forget to dispose the Bitmap returned from this method.

It does not work very well if users are uploading very large images (e.g. > 5000 * 5000 pixels) though. If that is a requirement, I would recommend using ImageMagick or a similar imaging package.

To accept png and tiff, just adjust how You check the file extension so that You accept "png", "tif" and "tiff" as well. Support for tiff is a bit sketchy in .NET though since tiff itself is only a container for MANY different encodings.

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