怎样用 c# GDI+ 截取一张图片的一部分,然后再替换另一张图片的一部分啊?求代码~!
1个回答
展开全部
主要利用://在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
image:你的原图,
System.Drawing.Rectangle:位置与长宽,根据自己需要设置!
--------------------------------------------------------------------------------------------------------------
实例如下:
public void MakeSmallImg(string filePath, string saveImg)
{
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true);
//取得图片大小
System.Drawing.Size size = new System.Drawing.Size((int)image.Width, (int)image.Height);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
//清空一下画布
g.Clear(System.Drawing.Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存高清晰度的缩略图
// bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
// 加个a表示是缩略图
bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
bitmap.Dispose();
}
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
image:你的原图,
System.Drawing.Rectangle:位置与长宽,根据自己需要设置!
--------------------------------------------------------------------------------------------------------------
实例如下:
public void MakeSmallImg(string filePath, string saveImg)
{
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true);
//取得图片大小
System.Drawing.Size size = new System.Drawing.Size((int)image.Width, (int)image.Height);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
//清空一下画布
g.Clear(System.Drawing.Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存高清晰度的缩略图
// bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
// 加个a表示是缩略图
bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
bitmap.Dispose();
}
追问
谢啦,我只是想做个电子钟。没看明白啊,你直接说哪个是截图的,哪个是换图哈?截图图片部分,然后把它替换掉另一张图片的指定位置。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询