当前位置:编程学习 > C#/ASP.NET >>

怎么对图片裁剪

我现在想做了个图片裁剪,比如原图片h =500px  w= 500px ,但需要裁图中的 h =200px  w= 100px 部分怎么处理呢 --------------------编程问答-------------------- 即就是上传图片后进行局部的图像裁剪,并保存为文件 这种功能 --------------------编程问答-------------------- 等比例缩放图片 
<script language="JavaScript"> 
var flag=false; 
function DrawImage(ImgD,iwidth,iheight){ 
    var image=new Image(); 
    image.src=ImgD.src; 
    if(image.width>0 && image.height>0){ 
    flag=true; 
    if(image.width/image.height>= iwidth/iheight){ 
        if(image.width>iwidth){  
        ImgD.width=iwidth; 
        ImgD.height=(image.height*iwidth)/image.width; 
        }else{ 
        ImgD.width=image.width;  
        ImgD.height=image.height; 
        } 
        ImgD.alt=image.width+"×"+image.height; 
        } 
    else{ 
        if(image.height>iheight){  
        ImgD.height=iheight; 
        ImgD.width=(image.width*iheight)/image.height;        
        }else{ 
        ImgD.width=image.width;  
        ImgD.height=image.height; 
        } 
        ImgD.alt=image.width+"×"+image.height; 
        } 

    } 

onload="javascript:DrawImage1(this)"> 
上传图片生成缩略图 
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; 

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(System.Drawing.Color.Transparent); 
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 
new System.Drawing.Rectangle(x, y, ow, oh), 
System.Drawing.GraphicsUnit.Pixel); 

try 

bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 

catch (System.Exception e) 

throw e; 

finally 

originalImage.Dispose(); 
bitmap.Dispose(); 
g.Dispose(); 


--------------------编程问答-------------------- 跟着如梦走,知识天天有 --------------------编程问答--------------------
using System.Drawing;
using System.Drawing.Drawing2D;

protected void Page_Load(object sender, EventArgs e)
{
    System.Drawing.Image img = Test("C:\\1.jpg", 0, 0, 200, 100);
    img.Save("C:\\2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    img.Dispose();
}
public System.Drawing.Image Test(string imgPath, int x, int y, int width, int height)
{
    System.Drawing.Image xImg = System.Drawing.Image.FromFile(imgPath);
    System.Drawing.Image oImg = new Bitmap(width, height, xImg.PixelFormat);
    Graphics g = Graphics.FromImage(oImg);
    g.CompositingQuality = CompositingQuality.HighSpeed;
    g.SmoothingMode = SmoothingMode.HighSpeed;
    g.InterpolationMode = InterpolationMode.HighQualityBilinear;

    g.DrawImage(xImg, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
    xImg.Dispose();

    return oImg;
}
--------------------编程问答-------------------- 楼上都贴了  我就不贴了,呵呵 --------------------编程问答-------------------- 不错
每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分    --------------------编程问答--------------------
 /// <summary>
        /// 裁剪
        /// </summary>
        /// <param name="xPosition">X起始点</param>
        /// <param name="yPosition">Y起始点</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        public void DrawOutCropArea(int xPosition, int yPosition, int width, int height)
        {
            _bitmapPrevCropArea = (Bitmap)_currentBitmap;
            Bitmap bmap = (Bitmap)_bitmapPrevCropArea.Clone();
            Graphics gr = Graphics.FromImage(bmap);
            Brush cBrush = new Pen(Color.FromArgb(150, Color.White)).Brush;
            Rectangle rect1 = new Rectangle(0, 0, _currentBitmap.Width, yPosition);
            Rectangle rect2 = new Rectangle(0, yPosition, xPosition, height);
            Rectangle rect3 = new Rectangle(0, (yPosition + height), _currentBitmap.Width, _currentBitmap.Height);
            Rectangle rect4 = new Rectangle((xPosition + width), yPosition, (_currentBitmap.Width - xPosition - width), height);
            gr.FillRectangle(cBrush, rect1);
            gr.FillRectangle(cBrush, rect2);
            gr.FillRectangle(cBrush, rect3);
            gr.FillRectangle(cBrush, rect4);
            _currentBitmap = (Bitmap)bmap.Clone();
        }
--------------------编程问答-------------------- UploadAndCrop 图片裁剪 http://www.cnblogs.com/zengxiangzhan/archive/2009/12/27/1633644.html
图片裁剪 PhotoCropper 
http://www.cnblogs.com/zengxiangzhan/archive/2010/01/04/1638780.html --------------------编程问答--------------------
引用 8 楼 zengzhan 的回复:
UploadAndCrop 图片裁剪 http://www.cnblogs.com/zengxiangzhan/archive/2009/12/27/1633644.html
图片裁剪 PhotoCropper
http://www.cnblogs.com/zengxiangzhan/archive/2010/01/04/1638780.html

谢谢我马上测试一下 --------------------编程问答-------------------- 我想,问一下,怎么给要打水印文字换行.
谢谢!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,