从数据库映像制作缩略图,同时保持宽高比 [英] make thumbnail from database image while keeping aspect ratio

查看:132
本文介绍了从数据库映像制作缩略图,同时保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经存储在我的SQL服务器中的图像,并imageContent是一个字节数组(其中包含存储在数据库图像数据)。我用下面的code从我的主图像创建缩略图,我目前集显的宽度和高度为我的缩略图(40×40),但它会破坏我的图像纵横比,我怎么能找到原始图像和规模的长宽比这倒使我的原始宽高比是不变的?

 流海峡=新的MemoryStream((字节[])imageContent);
        位图loBMP =新位图(STR);
        位图bmpOut =新位图(40,40);
        图形G = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White,0,0,40,40);
        g.DrawImage(loBMP,0,0,40,40);
        MemoryStream的毫秒=新的MemoryStream();
        bmpOut.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
        字节[] = bmpBytes ms.GetBuffer();
        bmpOut.Dispose();
        // END新        Response.ContentType = img_type;
        Response.BinaryWrite(bmpBytes); // imageContent,bmpBytes


解决方案

也许这可以帮助你:

 公共静态位图CreateThumbnail(位图源,诠释thumbWidth,诠释thumbHeight,布尔maintainAspect)
{
        如果(source.Width< thumbWidth&放大器;&安培; source.Height< thumbHeight)收益来源;        位图图像= NULL;
        尝试
        {
            INT宽度= thumbWidth;
            INT高度= thumbHeight;            如果(maintainAspect)
            {
                如果(source.Width> source.Height)
                {
                    宽度= thumbWidth;
                    身高=(INT)(source.Height *((十进制)thumbWidth / source.Width));
                }
                其他
                {
                    高度= thumbHeight;
                    宽度=(INT)(source.Width *((十进制)thumbHeight / source.Height));
                }
            }            图像=新位图(宽,高);
            使用(图形G = Graphics.FromImage(图片))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White,0,0,宽度,高度);
                g.DrawImage(源,0,0,宽度,高度);
            }            返回形象;
        }
        抓住
        {
            图像= NULL;
        }
        最后
        {
            如果(形象!= NULL)
            {
                image.Dispose();
            }
        }        返回null;
}

I've stored my images in SQL server, and imageContent is a byte array (which contains image data stored in DB). I use following code to create a thumbnail from my main image, currently I set explicit width and height for my thumbnail (40x40), but it will damage my image aspect ratio, how can I find aspect ratio of original image and scale down it so that my original aspect ratio is unchanged?

            Stream str = new MemoryStream((Byte[])imageContent);
        Bitmap loBMP = new Bitmap(str);
        Bitmap bmpOut = new Bitmap(40, 40);
        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, 40, 40);
        g.DrawImage(loBMP, 0, 0, 40, 40);
        MemoryStream ms = new MemoryStream();
        bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] bmpBytes = ms.GetBuffer();
        bmpOut.Dispose();
        //end new

        Response.ContentType = img_type;
        Response.BinaryWrite(bmpBytes);//imageContent,bmpBytes

解决方案

Probably this can help you:

public static Bitmap CreateThumbnail(Bitmap source, int thumbWidth, int thumbHeight, bool maintainAspect)
{
        if (source.Width < thumbWidth && source.Height < thumbHeight) return source;

        Bitmap image = null;
        try
        {
            int width = thumbWidth;
            int height = thumbHeight;

            if (maintainAspect)
            {
                if (source.Width > source.Height)
                {
                    width = thumbWidth;
                    height = (int)(source.Height * ((decimal)thumbWidth / source.Width));
                }
                else
                {
                    height = thumbHeight;
                    width = (int)(source.Width * ((decimal)thumbHeight / source.Height));
                }
            }

            image = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(image))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, width, height);
                g.DrawImage(source, 0, 0, width, height);
            }

            return image;
        }
        catch
        {
            image = null;
        }
        finally
        {
            if (image != null)
            {
                image.Dispose();
            }
        }

        return null;
}

这篇关于从数据库映像制作缩略图,同时保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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