日期:2014-05-18  浏览次数:20472 次

CSDN没高手了?
DataList里面图片显示失真变型,要怎么处理?

------解决方案--------------------
用js在前台按比例缩放一下.

JScript code

function PageLoadImg()
{
   var imgObj = new Image();
imgObj.src=$("ImgShow").src;

if(imgObj.width>imgObj.height)
{
var rw=imgObj.width;
var rh=$("ImgShow").height;
$("ImgShow").width=258;
$("ImgShow").height=($("ImgShow").width/rw)*rh;
}
else
if(imgObj.width<imgObj.height)
{
var rh=imgObj.height;
var rw=$("ImgShow").width;
$("ImgShow").height=258;
$("ImgShow").width=($("ImgShow").height/rh)*rw;
}
else
{
var rw=imgObj.width;
var rh=$("ImgShow").height;
$("ImgShow").width=258;
$("ImgShow").height=($("ImgShow").width/rw)*rh;
}

}

------解决方案--------------------
cs里面等比例缩放:
C# code
            string pic = context.Request["pic"];
            int W = 100;
            int H = 100;
            try
            {
                 W = int.Parse(context.Request["W"]);
                 H = int.Parse(context.Request["H"]);
            }
            catch
            {
                context.Response.End();
            }
            if(pic==null || pic=="" || W<=0 || H<=0 || pic.IndexOf(".")<1)
            {
                context.Response.End();
            }
            
            
            Bitmap bit = new Bitmap(System.Configuration.ConfigurationManager.AppSettings["UpLoadFile"] + pic.Replace("/upload/", "").Replace("/images/",""));
            MemoryStream ms = new MemoryStream();
            int newh = 0;
            int neww = 0;
            //
            if (bit.Width < W && bit.Height < H)
            {
                newh = bit.Height;
                neww = bit.Width;
            }
            else
            {
                double pwidth = (double)bit.Width / W;
                double pheight = (double)bit.Height / H;
                if (pwidth > pheight)
                {
                    neww = W;
                    newh = Convert.ToInt32(bit.Height / pwidth);
                }
                else
                {
                    newh = H;
                    neww = Convert.ToInt32(bit.Width / pheight);
                }
            }
            //生成小图
            Bitmap newbit = new Bitmap(bit, neww, newh);
            newbit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ContentType = "image/jpeg";
            context.Response.StatusCode = 200;
            context.Response.BinaryWrite(ms.ToArray());