| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 684 人关注过本帖, 1 人收藏
标题:C#剪切问题
只看楼主 加入收藏
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:4 
C#剪切问题
/// <summary>
        /// 剪切图片的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShear_Click(object sender, EventArgs e)
        {
            b = true;
        }

        /// <summary>
        /// 获取按下鼠标指针的坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (b)
            {
                this.Cursor = Cursors.Cross;
                p1 = new Point(e.X, e.Y);
            }
        }

        /// <summary>
        /// 记录鼠标的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (b)
            {
                if (this.Cursor == Cursors.Cross)
                {
                    p2 = new Point(e.X, e.Y);
                    pictureBox.Invalidate();
                }
            }
        }

        /// <summary>
        /// 获取松开鼠标时指针的坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (b)
            {
                this.Cursor = Cursors.Default;
                p2 = new Point(e.X, e.Y);
                CutImage();
                b = false;
            }
        }

        /// <summary>
        /// 记录鼠标在PictureBox上面画的矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (b)
            {
                Pen p = new Pen(Color.White, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                Rectangle rect = new Rectangle(p1, new Size(p2.X-p1.X,p2.Y-p1.Y));
                e.Graphics.DrawRectangle(p, rect);
                p.Dispose();
            }
        }

        /// <summary>
        /// 剪切图片
        /// </summary>
        private void CutImage()
        {
            try
            {
                img = this.pictureBox.Image;
                bt1 = new Bitmap(p2.X - p1.X, p2.Y - p1.Y);
                Rectangle tgtRect = new Rectangle(0, 0, p2.X - p1.X, p2.Y - p1.Y);
                Rectangle srcRect = new Rectangle(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                Graphics g = Graphics.FromImage(bt1);
                g.DrawImage(img, tgtRect, srcRect, GraphicsUnit.Pixel);
                this.pictureBox.Image = bt1;
                g.Dispose();
                pictureBox.Refresh();
            }
            catch(Exception)
            {

            }
        }

剪切跑位怎么处理啊?
搜索更多相关主题的帖子: 剪切 
2009-07-25 11:20
乖老鼠
Rank: 5Rank: 5
来 自:四川省
等 级:职业侠客
威 望:2
帖 子:434
专家分:394
注 册:2008-9-8
收藏
得分:14 
剪切跑位是什么意思?就是剪切框选好了,手动移动位置?

转眼就从编程菜鸟混成了半灌水
2009-07-25 11:58
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
收藏
得分:0 
乖老鼠
就是我用鼠标在图片中画出了要剪切的位子,然后松开鼠标,但是剪切出来的图片不是我用鼠标画出来的
2009-07-25 12:01
onionforce
Rank: 1
等 级:新手上路
帖 子:8
专家分:3
注 册:2009-7-7
收藏
得分:0 
获得鼠标拖动时的两个点p1\p2
Bitmap bmp1=Bitmap.Fromfile(.....)
int width=p2.X-p1.X;
int height=p1.Y-p2.Y;
Bitmap bmp2=new Bitmap(width,height);
for(int x,x<=width,x++)
{
    for(int y,y<=height,y++)
    {
          //用Getpixel获取bmp1的像素颜色,然后set到bmp2
    }
}
//最后把bmp2保存为相应的位置和格式即可
2009-08-05 09:13
onionforce
Rank: 1
等 级:新手上路
帖 子:8
专家分:3
注 册:2009-7-7
收藏
得分:0 
逐列扫描,O(∩_∩)O~
2009-08-05 09:14
快速回复:C#剪切问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036038 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved