本帖最后由 dxsh126 于 2009-10-20 14:52:35 编辑

解决方案 »

  1.   

    不能上图,就是用drawimage绘制一张图片的某一部分
      

  2.   

    #region 剪切图片
    public static bool CropImage(string photoName,int x,int y,int width,int height)
    {
    System.Drawing.Image image = null;
    System.Drawing.Image image_TM = null;
    System.Drawing.Image image_Temp = null;
    System.Drawing.Image image_TS = null;
    try
    {

    string filePath =System.Web.HttpContext.Current.Server.MapPath(photoName); string ext = System.IO.Path.GetExtension(filePath).ToLower(); image = System.Drawing.Image.FromFile(filePath); 
    image_Temp = ImageResize.Crop(image,x,y,width,height);
    image_TS = ImageResize.FixedSize(image_Temp,AlbumConstants.Thumbnail_S_Max,AlbumConstants.Thumbnail_S_Mix,true);
    image_TM = ImageResize.FixedSize(image_Temp,AlbumConstants.Thumbnail_M_Max,AlbumConstants.Thumbnail_M_Min,false); System.Drawing.Imaging.ImageFormat imgf = System.Drawing.Imaging.ImageFormat.Bmp;
    switch (ext) 
    {
    case ".bmp":
    ext = ".bmp";
    imgf = System.Drawing.Imaging.ImageFormat.Bmp;
    break;
    case ".jpg":
    ext = ".jpg";
    imgf = System.Drawing.Imaging.ImageFormat.Jpeg;
    break;
    case ".jpeg":
    ext = ".jpeg";
    imgf = System.Drawing.Imaging.ImageFormat.Jpeg;
    break;
    case ".png":
    ext = ".png";
    imgf = System.Drawing.Imaging.ImageFormat.Png;
    break;
    case ".gif":
    ext = ".gif";
    imgf = System.Drawing.Imaging.ImageFormat.Gif;
    break;
    default:
    ext = ".bmp";
    imgf = System.Drawing.Imaging.ImageFormat.Bmp;
    break;
    }

    image.Dispose();
    string strFileName_TS = filePath.Replace(AlbumConstants.SymbolOfThumbnailImage_M,AlbumConstants.SymbolOfThumbnailImage_S);
    image_TS.Save(strFileName_TS,imgf);
    image_TM.Save(filePath,imgf);
    }
    catch(Exception error)
    {
    throw error;
    }
    finally
    {
    if(image!= null)
    image.Dispose();
    if(image_TM!= null)
    image_TM.Dispose();
    if(image_TS!= null)
    image_TS.Dispose();
    if(image_Temp !=null)
    image_Temp.Dispose();
    }
    return true;
    }
    #endregion 剪切图片
      

  3.   

    public static Image Crop(Image imgPhoto,int x,int y,int width,int height)
    {
    int sourceWidth = imgPhoto.Width;//原图片的宽
    int sourceHeight = imgPhoto.Height;//原图片的高 int ProcessWidth  = 0;//处理图片的宽
    int ProcessHeight = 0;//处理图片的高 int destWidth  = 0;//剪切时的宽
    int destHeight = 0;//剪切时的高 int sourceX = 0;
    int sourceY = 0; int destX = 0;//剪切时x坐标
    int destY = 0;//剪切时y坐标 float nPercent = 0; if(sourceWidth >  sourceHeight)//计算处理图片的宽和高
    {
    ProcessWidth = AlbumConstants.ProcessImg_Width;
    nPercent = ((float)AlbumConstants.ProcessImg_Width/(float)sourceWidth);
    ProcessHeight = (int)(sourceHeight * nPercent);
    }
    else
    {
    ProcessHeight = AlbumConstants.ProcessImg_Width;
    nPercent = ((float)AlbumConstants.ProcessImg_Width/(float)sourceHeight);
    ProcessWidth = (int)(sourceWidth * nPercent);
    } nPercent = ((float)sourceWidth/(float)ProcessWidth);//计算原图x坐标
    destX = (int)(nPercent * x); nPercent = ((float)sourceHeight/(float)ProcessHeight);//计算原图x坐标
    destY = (int)(nPercent * y); nPercent = ((float)sourceHeight/(float)ProcessHeight);//计算原图高
    destHeight = (int)(nPercent * height); nPercent = ((float)sourceWidth/(float)ProcessWidth);//计算原图宽
    destWidth = (int)(nPercent * width);
    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(sourceX,sourceY,destWidth,destHeight),
    new Rectangle(destX,destY,destWidth,destHeight),
    GraphicsUnit.Pixel); grPhoto.Dispose();
    return bmPhoto; }
      

  4.   

    drawimage,描述上写的“在指定位置并且按指定大小绘制指定的 Image 的指定部分”
    如何用这个实现的?需要操做的图片内容很多……
      

  5.   

    搞定了
    Graphics.DrawImage 方法 (Image, Point[], Rectangle, GraphicsUnit)
    msdn上的描述也有部分错误的, 害我一个一个试。
      

  6.   

    饿,是这个方法。上面那个应该也行Graphics.DrawImage 方法 (Image, Rectangle, Rectangle, GraphicsUnit)