C#图片操作的问题,求教、 有一张图片是1.jpg. widht:100 height:200现在我想得到这张图片的x坐标10 ,y坐标40的位置,长为40,宽为70的图片区域,并在<img src='1.jpg' ></img>上显示出截取后的图片 ,请问如何实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/t/20050413/11/3932437.html /// <summary> /// 从图片中截取部分生成新图 /// </summary> /// <param name="sFromFilePath">原始图片</param> /// <param name="saveFilePath">生成新图</param> /// <param name="width">截取图片宽度</param> /// <param name="height">截取图片高度</param> /// <param name="spaceX">截图图片X坐标</param> /// <param name="spaceY">截取图片Y坐标</param> public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY) { //载入底图 Image fromImage = Image.FromFile(sFromFilePath); int x = 0; //截取X坐标 int y = 0; //截取Y坐标 //原图宽与生成图片宽 之差 //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者 即原图宽度 X坐标则为0 //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值 即width X坐标则为 sX与spaceX之间较小者 //Y方向同理 int sX = fromImage.Width - width; int sY = fromImage.Height - height; if (sX > 0) { x = sX > spaceX ? spaceX : sX; } else { width = fromImage.Width; } if (sY > 0) { y = sY > spaceY ? spaceY : sY; } else { height = fromImage.Height; } //创建新图位图 Bitmap bitmap = new Bitmap(width, height); //创建作图区域 Graphics graphic = Graphics.FromImage(bitmap); //截取原图相应区域写入作图区 graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel); //从作图区生成新图 Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap()); //保存图象 saveImage.Save(saveFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); //释放资源 saveImage.Dispose(); bitmap.Dispose(); graphic.Dispose(); }重绘 protected void Page_Load(object sender, EventArgs e) { string path = Server.MapPath("images/p002.jpg"); DrawImage(path, 10, 40, 40, 70); } public void DrawImage(string path, int x, int y, int width, int height) { string savepath = "images/thu_p002.jpg"; System.Drawing.Image image = System.Drawing.Image.FromFile(path); Bitmap bitmap = new Bitmap(width, height); if (image.Width - width > 0) x = x > (image.Width - width) ? image.Width - width : x; else width = image.Width; if (image.Height - height > 0) y = y > (image.Height - height) ? image.Height - height : y; else height = image.Height; Graphics g = Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel); bitmap.Save(Server.MapPath(savepath)); bitmap.Dispose(); image.Dispose(); g.Dispose(); Image1.ImageUrl = savepath; } 带有html代码的字符串插入数据库后被自动编码? 怎么把string转换成IList类型?? 在别人已经做好的项目的基础上修改,首先应该怎么做呢?挺着急的。。希望大家能帮帮忙。。 WEB数据导出到excell(图片)问题 ★☆★第一次散分(1)★☆★ 问一个问题:用户注册时提示使用的什么控件? 接收外网邮件怎样得到邮件内容和附件? 帮帮我! treeview的格式问题 为什么是这样子的呀? 在DataSet中没有找到数据,怎么让他显示没有找到数据 贴出需求,求论坛模板(学生贴) C#通过程序批量添加数据
/// 从图片中截取部分生成新图
/// </summary>
/// <param name="sFromFilePath">原始图片</param>
/// <param name="saveFilePath">生成新图</param>
/// <param name="width">截取图片宽度</param>
/// <param name="height">截取图片高度</param>
/// <param name="spaceX">截图图片X坐标</param>
/// <param name="spaceY">截取图片Y坐标</param>
public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)
{
//载入底图
Image fromImage = Image.FromFile(sFromFilePath);
int x = 0; //截取X坐标
int y = 0; //截取Y坐标
//原图宽与生成图片宽 之差
//当小于0(即原图宽小于要生成的图)时,新图宽度为较小者 即原图宽度 X坐标则为0
//当大于0(即原图宽大于要生成的图)时,新图宽度为设置值 即width X坐标则为 sX与spaceX之间较小者
//Y方向同理
int sX = fromImage.Width - width;
int sY = fromImage.Height - height;
if (sX > 0)
{
x = sX > spaceX ? spaceX : sX;
}
else
{
width = fromImage.Width;
}
if (sY > 0)
{
y = sY > spaceY ? spaceY : sY;
}
else
{
height = fromImage.Height;
} //创建新图位图
Bitmap bitmap = new Bitmap(width, height);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
//保存图象
saveImage.Save(saveFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放资源
saveImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}
重绘
{
string path = Server.MapPath("images/p002.jpg");
DrawImage(path, 10, 40, 40, 70);
}
public void DrawImage(string path, int x, int y, int width, int height)
{
string savepath = "images/thu_p002.jpg";
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Bitmap bitmap = new Bitmap(width, height);
if (image.Width - width > 0)
x = x > (image.Width - width) ? image.Width - width : x;
else
width = image.Width;
if (image.Height - height > 0)
y = y > (image.Height - height) ? image.Height - height : y;
else
height = image.Height;
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
bitmap.Save(Server.MapPath(savepath));
bitmap.Dispose();
image.Dispose();
g.Dispose();
Image1.ImageUrl = savepath;
}