public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height, int offsetX, int offsetY)
{
Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
Bitmap resultBitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
Rectangle resultRectangle = new Rectangle(0, 0, Width, height);
Rectangle sourceRectangle = new Rectangle(0+offsetX, 0+offsetY, Width, height);
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return resultBitmap;
}
http://www.cnblogs.com/NRabbit/archive/2009/07/19/1736174.html以上代码没有问题,但是指定X,Y坐标后剪切的部分其实没有剪切而是空白。。怎么把空白去掉
{
Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
Bitmap resultBitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
Rectangle resultRectangle = new Rectangle(0, 0, Width, height);
Rectangle sourceRectangle = new Rectangle(0+offsetX, 0+offsetY, Width, height);
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return resultBitmap;
}
http://www.cnblogs.com/NRabbit/archive/2009/07/19/1736174.html以上代码没有问题,但是指定X,Y坐标后剪切的部分其实没有剪切而是空白。。怎么把空白去掉
解决方案 »
- 高分求解不同网段的socket通信问题
- C#如何弹出选择可用盘符对话框
- 字符串的问题(在线等)
- .NET安装问题,解决立即结帖
- Server Error in '/' Application.
- 好多人都没能解决的问题?有关DataGrid分页.
- 急!!!java里的UUID和.net里的GUID
- C/S结构的程序中如何在客户端获得服务器端的系统日期(急)
- c#中窗体调用的问题?
- 今天发现从ftp://qydn.vicp.net/下载的msdn是February版本的,而不是很多说安装成功者所使用的April版本,所以估计这也是很多人下了msdn但
- 无法将类型“System.EventHandler”隐式转换为“System.Windows.Forms.MouseEventHandler”
- 求一正则删除“=”号后的空格。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using S10806Class;
using System.IO;namespace GetImage
{
public partial class GetScreenImgForm : Form
{ /// <summary>
/// 图像操作类
/// </summary>
ImageWork imageWork = new ImageWork(); /// <summary>
/// 保存截取到的桌面图像
/// </summary>
Image screenImg = null; /// <summary>
/// 保存用户截取的图像区域
/// </summary>
Rectangle imgRect = new Rectangle(); /// <summary>
/// 窗体的GDI
/// </summary>
Graphics g = null; /// <summary>
/// 区域边框画笔
/// </summary>
Pen pen = new Pen(Color.Black, 1); /// <summary>
/// 是否应该绘制区域
/// </summary>
bool isDraw = false; /// <summary>
/// 用户是否选择了区域
/// </summary>
bool isOption = false;
public GetScreenImgForm()
{
InitializeComponent(); //获取当前桌面截图
screenImg = imageWork.GetScreenImage(); g = this.CreateGraphics();
} private void GetScreenImgForm_Load(object sender, EventArgs e)
{
this.Size = new Size(screenImg.Width, screenImg.Height); this.BackgroundImage = screenImg;
} private void GetScreenImgForm_MouseUp(object sender, MouseEventArgs e)
{
//没有截图并且单击右键 - 关闭本窗体
if (e.Button == MouseButtons.Right && !this.isOption)
{
this.Close();
}
//截图了并且单击右键 - 取消此次截图
else if (e.Button == MouseButtons.Right && this.isOption)
{
g.DrawImage(screenImg, new Point(0, 0));
this.isOption = false;
return;
}
//在截图情况下松开左键,选择区域结束,等待双击截取
else if (e.Button == MouseButtons.Left && this.isOption && this.isDraw)
{
this.isDraw = false; imgRect.Width = e.X - imgRect.X; //区域长度 imgRect.Height = e.Y - imgRect.Y; //区域高度
} } private void GetScreenImgForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return; if (this.isOption) return; imgRect.X = e.X; //起始X轴 imgRect.Y = e.Y; //起始Y轴 this.isDraw = true;//允许绘图
} private void GetScreenImgForm_MouseMove(object sender, MouseEventArgs e)
{
if (isDraw)
{
//刷新图像
g.DrawImage(screenImg, new Point(0, 0)); this.isOption = true; Point p1 = new Point(imgRect.X, imgRect.Y);
Point p2 = new Point(e.X, imgRect.Y);
Point p3 = new Point(imgRect.X, e.Y);
Point p4 = new Point(e.X, e.Y); //画四条边框
g.DrawLine(pen, p1, p2);
g.DrawLine(pen, p2, p4);
g.DrawLine(pen, p4, p3);
g.DrawLine(pen, p3, p1);
}
else
{
//确定用户的选择区域
int top = imgRect.Y;
int down = imgRect.Y + imgRect.Height;
int left = imgRect.X;
int right = imgRect.X + imgRect.Width; //鼠标在选择区域中会变个模样
if (e.X > left && e.X < right && e.Y > top && e.Y < down)
{
this.Cursor = Cursors.SizeAll;
}
else
{
this.Cursor = Cursors.Cross;
}
}
} private void GetScreenImgForm_MouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
//用户双击选择区域时.....
if (this.Cursor == Cursors.SizeAll)
{
Bitmap b = new Bitmap(this.screenImg); Bitmap img = b.Clone(this.imgRect, System.Drawing.Imaging.PixelFormat.DontCare); SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "(*.jpg)|*.jpg"; if (dlg.ShowDialog() != DialogResult.OK) return; img.Save(dlg.FileName); this.Close();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
} }
}
{
Bitmap bmSplit = SplitBitmap(@"C:\Documents and Settings\Administrator\桌面\备用\饼状图.jpg", 200, 20, 10, 10);
this.picbShow.Image = bmSplit;
this.picbShow.BorderStyle = BorderStyle.FixedSingle;
} public Bitmap SplitBitmap(string PbmPath, int Pwid, int Phei, int PoffsetX, int PoffsetY)
{
Bitmap bmSource = new Bitmap(PbmPath); Bitmap bmNew = new Bitmap(bmSource);
bmSource.Dispose();
bmSource = null; Bitmap bmSplit = new Bitmap(Pwid, Phei);
Graphics g = Graphics.FromImage(bmSplit);
g.DrawImage(bmNew, new Rectangle(PoffsetX, PoffsetY, Pwid, Phei));
g.Dispose();
g = null;
bmNew.Dispose();
bmNew = null; return bmSplit;
}
经过测试完全正确.
g.DrawImage(bmNew, new Rectangle(0, 0, Pwid, Phei), new Rectangle(PoffsetX, PoffsetY, Pwid, Phei), System.Drawing.GraphicsUnit.Pixel);
额... 那句话改成这个...