目的是在输入框中输入一个图片的URL,然后在本目录内生成一个缩略图并显示出来, 
这个缩略图要求是海军蓝色的背景色,大小为50×50,URL中的图片按比例缩小并在缩略图中居中放置,其它已经可以了,但这个代码不会按比例缩小也不会居中,实在搞不定了,觉得C#真的不太好用,把GDI的API封装得乱七八糟using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;namespace GImage
{
/// <summary>
/// _default 的摘要说明。
/// </summary>
public class _default : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
      this.Button1.Click += new System.EventHandler(this.Button1_Click);
      this.Load += new System.EventHandler(this.Page_Load);    }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
      string sUrl = TextBox1.Text;
int i = sUrl.LastIndexOf("/") + 1;
string str = sUrl.Substring(i, sUrl.Length - i);
WebRequest webRequest = WebRequest.Create(sUrl);
webRequest.Credentials = CredentialCache.DefaultCredentials;
Stream stream = webRequest.GetResponse().GetResponseStream(); MemoryStream memoryStream = new MemoryStream();
byte[] bs = new byte[256];
for ( int j = stream.Read(bs, 0, (int)bs.Length); j > 0; j = stream.Read(bs, 0, (int) bs.Length))
{
memoryStream.Write(bs, 0, j); //将缓冲区数据写入memoryStream内存流
}
stream.Close();
memoryStream.Position = (long)0;
      string filepath = Server.MapPath(".") + "\\";
string fileName = getRand().ToString() + ".jpg";
      string fileFullPath = @filepath + fileName;
      System.Drawing.Image image = new Bitmap(memoryStream);
      getThumbImage(image, new Size(50,50), fileFullPath);
      image.Dispose();
      Image1.ImageUrl = fileName;
} private void getThumbImage(System.Drawing.Image image, Size targetSize, string filePath)
{
      int x, y, tx, ty;
System.Drawing.Image TargetBMP = new Bitmap(image, targetSize); //创建一个BMP图像对象
System.Drawing.Graphics TargetGraphics = System.Drawing.Graphics.FromImage(TargetBMP);  //转换为Graphics对象
      //TargetGraphics.Clear(Color.Navy);
TargetGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //高质量插值法
TargetGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  //平滑处理      if (image.Height >= image.Width)
      {
        y = TargetBMP.Height - 4;
        x = ((y * image.Width) / (image.Height));
        tx = ((image.Width - x) / 2);
        ty = 2;
      }
      else
      {
        x = TargetBMP.Width - 4;
        y = (x * image.Height / image.Width);
        tx = 2;
        ty = ((TargetBMP.Height - y) / 2);
      }
    
      TargetGraphics.DrawImage(TargetBMP,
        new System.Drawing.Rectangle(tx, ty, x, y),
        new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
        System.Drawing.GraphicsUnit.Pixel); //使用DrawImage比使用GetThumbnailImage得到的图像质量更高
      TargetBMP.Save(filePath, ImageFormat.Jpeg);
TargetGraphics.Dispose();
      TargetBMP.Dispose();
}    private int getRand()
    {
      Random rnd = new Random();
      return rnd.Next(100000,999999); 
    }
    
}
}

解决方案 »

  1.   

    干嘛这么复杂,缩略图本来就是有个意思就行了,直接拿IMG输出它就得了。
      

  2.   

    '生成百分比缩略图
        Public Function GenerateThumbnail(ByVal original As Image, ByVal percentage As Integer) As Image
            If percentage < 1 Then
                Throw New Exception("Thumbnail size must be aat least 1% of the original size")
            End If
            Dim tn As New Bitmap(CInt(original.Width * 0.01F * percentage), CInt(original.Height * 0.01F * percentage))
            Dim g As Graphics = Graphics.FromImage(tn)
            g.InterpolationMode = g.InterpolationMode.HighQualityBilinear
            g.DrawImage(original, New Rectangle(0, 0, tn.Width, tn.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel)
            g.Dispose()
            Return CType(tn, Image)
        End Function    '生成指定大小缩略图
        Public Function SpecThumbnail(ByVal original As Image, ByVal x As Integer, ByVal y As Integer) As Image
            Dim callback As Image.GetThumbnailImageAbort
            original = original.GetThumbnailImage(x, y, callback, System.IntPtr.Zero)
            Return CType(original, Image)
        End Function
      

  3.   

    不行啊,改成这样后还是不行,Graphics.DrawImage到底怎么用?C#能不能直接用StretchBlt这个API函数啊,还是StretchBlt容易理解
    private void getThumbImage(System.Drawing.Image oImage, Size targetSize, string filePath)
    {
      int x, y, w, h;
      System.Drawing.Image TargetBMP = new Bitmap(oImage, targetSize); 
      System.Drawing.Graphics TargetGraphics = System.Drawing.Graphics.FromImage(TargetBMP);
      TargetGraphics.Clear(Color.Navy);
      TargetGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
      TargetGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;      if (oImage.Height >= oImage.Width)
          {
            y = 2;
            h = targetSize.Height - 4;
            w = h * oImage.Width / oImage.Height;
            x = (targetSize.Width - w) / 2;
          }
          else
          {
            x = 2;
            w = targetSize.Height - 4;
            h = w * oImage.Height / oImage.Width;
            y = (targetSize.Height - h) / 2;
          }
        
          TargetGraphics.DrawImage(TargetBMP,
            new System.Drawing.Rectangle(x, y, w, h),
            new System.Drawing.Rectangle(0, 0, oImage.Width, oImage.Height),
            System.Drawing.GraphicsUnit.Pixel);
          TargetBMP.Save(filePath, ImageFormat.Jpeg);
          TargetGraphics.Dispose();
          TargetBMP.Dispose();
        }