读书人

请教怎么将大图片缩小显示

发布时间: 2012-02-06 15:52:45 作者: rapoo

请问如何将大图片缩小显示?
请问如何将大图片缩小显示?
我试过用<asp:Image>和<asp:ImageButton>,指定它的宽度和高度,但只要图片大小超过指定的宽高,都会显示成原始大上,把页面弄得混乱不堪。而且图片太多,不可能全部处理成指定大小,所以上来请教,谢谢!

[解决办法]
应该不会.

<asp:image style="width:100px;height:100px" 不管图片多大都会按指定大小显示
[解决办法]
1 直接使用缩略图
2 将图片转换成一定大小再显示(比例,固定高度和宽度等)
[解决办法]

C# code
//代码摘自网络,自己定义个类,将相关代码放进去即可使用enum Dimensions         {            Width,            Height        }        enum AnchorPosition        {            Top,            Center,            Bottom,            Left,            Right        }static Image ScaleByPercent(Image imgPhoto, int Percent)        {            float nPercent = ((float)Percent/100);            int sourceWidth = imgPhoto.Width;            int sourceHeight = imgPhoto.Height;            int sourceX = 0;            int sourceY = 0;            int destX = 0;            int destY = 0;             int destWidth  = (int)(sourceWidth * nPercent);            int destHeight = (int)(sourceHeight * nPercent);            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);            Graphics grPhoto = Graphics.FromImage(bmPhoto);            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;            grPhoto.DrawImage(imgPhoto,                 new Rectangle(destX,destY,destWidth,destHeight),                new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),                GraphicsUnit.Pixel);            grPhoto.Dispose();            return bmPhoto;        }        static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)        {            int sourceWidth = imgPhoto.Width;            int sourceHeight = imgPhoto.Height;            int sourceX = 0;            int sourceY = 0;            int destX = 0;            int destY = 0;             float nPercent = 0;            switch(Dimension)            {                case Dimensions.Width:                    nPercent = ((float)Size/(float)sourceWidth);                    break;                default:                    nPercent = ((float)Size/(float)sourceHeight);                    break;            }                            int destWidth  = (int)(sourceWidth * nPercent);            int destHeight = (int)(sourceHeight * nPercent);            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);            Graphics grPhoto = Graphics.FromImage(bmPhoto);            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;            grPhoto.DrawImage(imgPhoto,             new Rectangle(destX,destY,destWidth,destHeight),            new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),            GraphicsUnit.Pixel);            grPhoto.Dispose();            return bmPhoto;        }        static Image FixedSize(Image imgPhoto, int Width, int Height)        {            int sourceWidth = imgPhoto.Width;            int sourceHeight = imgPhoto.Height;            int sourceX = 0;            int sourceY = 0;            int destX = 0;            int destY = 0;             float nPercent = 0;            float nPercentW = 0;            float nPercentH = 0;            nPercentW = ((float)Width/(float)sourceWidth);            nPercentH = ((float)Height/(float)sourceHeight);            //if we have to pad the height pad both the top and the bottom            //with the difference between the scaled height and the desired height            if(nPercentH < nPercentW)            {                nPercent = nPercentH;                destX = (int)((Width - (sourceWidth * nPercent))/2);            }            else            {                nPercent = nPercentW;                destY = (int)((Height - (sourceHeight * nPercent))/2);            }                    int destWidth  = (int)(sourceWidth * nPercent);            int destHeight = (int)(sourceHeight * nPercent);            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);            Graphics grPhoto = Graphics.FromImage(bmPhoto);            grPhoto.Clear(Color.Red);            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;            grPhoto.DrawImage(imgPhoto,                 new Rectangle(destX,destY,destWidth,destHeight),                new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),                GraphicsUnit.Pixel);            grPhoto.Dispose();            return bmPhoto;        } 


[解决办法]

C# code
using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;
[解决办法]
生成缩略图就行了..
C# code
using System;using System.Data;using System.Configuration;using System.Drawing;namespace MyClassLib{    /// <summary>    /// MakeThumbnail 的摘要说明    /// </summary>    public class MakeThumbnail    {        public MakeThumbnail()        {            //            // TODO: 在此处添加构造函数逻辑            //        }        /// <summary>        /// 生成缩略图        /// </summary>        /// <param name=\"originalImagePath\">源图路径(物理路径)</param>        /// <param name=\"thumbnailPath\">缩略图路径(物理路径)</param>        /// <param name=\"width\">缩略图宽度</param>        /// <param name=\"height\">缩略图高度</param>        /// <param name=\"mode\">生成缩略图的方式</param>           public static void Makethumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)        {            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);            int towidth = width;            int toheight = height;            int x = 0;            int y = 0;            int ow = originalImage.Width;            int oh = originalImage.Height;            switch (mode)            {                case "HW"://指定高宽缩放(可能变形)                                    break;                case "W"://指定宽,高按比例                                        toheight = originalImage.Height * width / originalImage.Width;                    break;                case "H"://指定高,宽按比例                    towidth = originalImage.Width * height / originalImage.Height;                    break;                case "Cut"://指定高宽裁减(不变形)                                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)                    {                        oh = originalImage.Height;                        ow = originalImage.Height * towidth / toheight;                        y = 0;                        x = (originalImage.Width - ow) / 2;                    }                    else                    {                        ow = originalImage.Width;                        oh = originalImage.Width * height / towidth;                        x = 0;                        y = (originalImage.Height - oh) / 2;                    }                    break;                default:                    break;            }            //新建一个bmp图片            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);            //新建一个画板            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);            //设置高质量插值法            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;            //设置高质量,低速度呈现平滑程度            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            //清空画布并以透明背景色填充            g.Clear(Color.Transparent);            //在指定位置并且按指定大小绘制原图片的指定部分            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),                new Rectangle(x, y, ow, oh),                GraphicsUnit.Pixel);            try            {                //以jpg格式保存缩略图                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);            }            catch (System.Exception e)            {                throw e;            }            finally            {                originalImage.Dispose();                bitmap.Dispose();                g.Dispose();            }        }    }} 

读书人网 >asp.net

热点推荐