我的论坛显示信息的地方只有500px 
当一个链接图片的宽度大过于500px的时候
如何对其进行调整为500px

解决方案 »

  1.   

    ASP.NET上传图片并生成可带版权信息的缩略图 <%@ Page Language="C#" ResponseEncoding="gb2312" %>
    <%@ import Namespace="System" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.Drawing" %>
    <%@ import Namespace="System.Drawing.Imaging" %>
    <script runat="server"> 
      void Page_Load(Object sender, EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    ImgPreview.Visible=false;
    }
    }
    void GetThumbnailImage(int width,int height,string strInfo,int left,int right)
    {
    string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1);
    string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1)+".jpg";
    string strAdd=strInfo;
    System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
    System.Drawing.Image thumbnailImage =
    oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
    Response.Clear();
    Bitmap output=new Bitmap(thumbnailImage);
    Graphics g=Graphics.FromImage(output);
    g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right);
    output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
    Response.ContentType = "image/gif";
    ImgPreview.Visible=true;
    ImgPreview.ImageUrl=newfile;
    }
    bool ThumbnailCallback()
    {
    return true;
    }
      

  2.   

    using System.Drawing;
    using System.Drawing.Imaging;namespace ifyr.util{
    class ImageHandler {
    static void Main(string[] args) {
    Image sourceImage = Image.FromFile(@"e:\source.jpg");Compress(sourceImage, @"e:\target.jpg");
    CreateThumb(sourceImage, 120, 160, @"e:\thumb.jpg");sourceImage.Dispose();
    }/// 
    /// 将图片转换成200k以下的图片
    /// 即生成800*800以内像素,65%品质的JPEG格式图片
    /// 请先判断图片大小,如果小于200k,则不需压缩,以免损失图片质量
    /// 可以改写为输出到OutputStream
    /// 
    /// 原图
    /// 压缩图片的保存文件名
    public static void Compress(Image sourceImage, string filename) {
    int targetWidth = 800;
    int targetHeight = 800;if(sourceImage.Height <= 800 && sourceImage.Width <= 800) {
    targetHeight = sourceImage.Height;
    targetWidth = sourceImage.Width;
    } else if(sourceImage.Height > sourceImage.Width) {
    targetWidth = sourceImage.Width * 800 / sourceImage.Height;
    } else {
    targetHeight = sourceImage.Height * 800 / sourceImage.Width;
    }// 缩放图片
    Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
    Graphics g = Graphics.FromImage(targetImage);
    g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
    g.Dispose();
    // 设置输出格式
    EncoderParameters encParams = new EncoderParameters(1);
    encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65L);
    ImageCodecInfo codeInfo = null;
    ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
    foreach(ImageCodecInfo info in codeInfos) {
    if(info.MimeType.Equals("image/jpeg")) {
    codeInfo = info;
    break;
    }
    }
    if(codeInfo != null) {
    targetImage.Save(filename, codeInfo, encParams);
    }
    targetImage.Dispose();
    }/// 
    /// 生成缩略图
    /// 可以改写成输出到OutputSteam
    /// 
    /// 原图
    /// 缩略图宽度
    /// 缩略图高度
    /// 缩略图的保存文件名
    public static void CreateThumb(Image sourceImage, int thumbWidth, int thumbHeight, string filename) {
    int tw = thumbWidth;
    int th = thumbHeight;if(sourceImage.Height < th && sourceImage.Width < tw) {
    th = sourceImage.Height;
    tw = sourceImage.Width;
    } else if(sourceImage.Height / th > sourceImage.Width / tw) {
    tw = sourceImage.Width * th / sourceImage.Height;
    } else {
    th = sourceImage.Height * tw / sourceImage.Width;
    }// 将图片缩放到一个空白背景的图片上,生成大小一致的缩略图
    Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppRgb).GetHbitmap());
    Graphics g = Graphics.FromImage(thumbImage);
    SolidBrush brush = new SolidBrush(Color.White);
    g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
    g.DrawImage(sourceImage, (thumbWidth - tw) / 2, (thumbHeight - th) / 2, tw, th);
    g.Dispose();thumbImage.Save(filename, ImageFormat.Jpeg);
    thumbImage.Dispose();
    }
    }
    }
      

  3.   

    以下代码放到网页内
    function imgzoom(o) 
    {
    if(event.ctrlKey) 
    {
    var zoom = parseInt(o.style.zoom, 10) || 100;
    zoom -= event.wheelDelta / 12;
    if(zoom > 0) 
    {
    o.style.zoom = zoom + '%';
    }
    return false;

    else 
    {
    return true;
    }
    }
    window.onload = Init;
    function Init()
    {
    //定义最大宽度
    var maxWidth = 550;
    var maxHeight = 350;

             //
    //2005-10-24 by 武眉博,huobazi  防止图片把文章内容页面撑大
             //(visit: http://www.aspxboy.com )
    //
    var imgs = document.getElementsByTagName("img");
    for(var i=0; i<imgs.length; i++)
    {
    var img = imgs[i];
    if (img.width > maxWidth)
    {
    img.width = maxWidth;
    if (img.parentElement.tagName != "A")
    {
    img.onclick = function(){window.open(this.src)}
    img.style.cursor = "pointer";
    img.style.cursor = "hand";
    //img.onmousewheel=function(){imgzoom(this);}
    //img.alt="点击看大图,或按住Ctrl键滚动鼠标滚轮放大或者缩小";
    img.alt=">>-请点击鼠标查看大图-<<";
    }
    }
    }
    }