请问在winform里,怎样在一个图片上面绘制一个区域

Navymk 2008-05-26 07:55:47
想让用户自己在图片上划一个区域做裁切.该区域应可以移动和改变大小等,类似ps中的crop功能.
谢谢!
...全文
353 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yatobiaf 2008-05-27
  • 打赏
  • 举报
回复
都结贴了阿……我刚才到项目中试了一下,回来就结贴了……什么世道阿,早知道不去试了……
yatobiaf 2008-05-27
  • 打赏
  • 举报
回复
可以用GDI来实现的。画一个矩形,然后用一把刷子填充这个矩形。这个刷子的颜色是RGBA格式的,最后一个A是alpha值,表示不透明值,你把这个值设成128(也就255的一半)就可以了。

//这是一个半透明蓝色的刷子,用这个刷子来刷矩形
Brush brush = new SolidBrush(Color.FromArgb(128, Color.Blue));
Navymk 2008-05-27
  • 打赏
  • 举报
回复
代码收藏了,谢谢.

已经解决,还是使用的picturebox.

透明的问题绕开了.

大致的思路是这样.

picturebox1原始图
picturebox2做区域划分,visiable为false

自定义一个cutimage方法,返回一个Image对象.

在picturebox1中的mousedown事件中,确定p2的位置,将picturebox2的visiable设置为true,置于p1的上方.
根据鼠标位置,确定对p1的Image的裁切位置,使用cutimage将切好的图片赋给p2,让p2的裁切图与下面的p1完全吻合模拟透明效果.
picturebox1的mousemove实现对p2拖拽大小.

...笨就笨点吧...总归实现了...还是用的自己的那几个老类...

感谢关注,感谢wzuomin,来者有分...总归也没几个人...

Navymk 2008-05-27
  • 打赏
  • 举报
回复
谢谢了.
我需要对层做拖拽移动/改变大小等多次修改,恐怕gdi绘制的矩形并不能满足要求.
wzuomin 2008-05-26
  • 打赏
  • 举报
回复
lz想要的这种效果其实可以通过显示一个Form(设置该Form的透明度)来实现。
但是这个Form需要设置无边框,并可以拖动及改变大小才行。
下面这个是我用过的方法,不妨试一下吧。呵呵

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace BrowserPlugins
{
public partial class FormCatcher : Form
{
public FormCatcher()
{
InitializeComponent();
}

public int x = 0;
public int y = 0;
public Rectangle r;

private void FormCatcher_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
Rectangle rect = new Rectangle(0, 0, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);
g.DrawRectangle(Pens.Red, rect);
}
}

private void FormCatcher_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.x = e.X;
this.y = e.Y;
r = this.Bounds;
}
}

private void FormCatcher_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x;
int dy = e.Y - y;
this.Location = new Point(this.Location.X + dx, this.Location.Y + dy);
}
}

private void FormCatcher_DoubleClick(object sender, EventArgs e)
{
Bitmap bmpCatched = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmpCatched);
g.CopyFromScreen(this.Location, new Point(0, 0), this.ClientRectangle.Size);
Clipboard.SetImage(bmpCatched);
MessageBox.Show("截图已经复制到剪切板!","info", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}

#region ReSize
private void FormCatcher_Resize(object sender, EventArgs e)
{
btn0.SetBounds(0,0,8,8);
btn1.SetBounds((this.Width-8)/2,0,8,8 );
btn2.SetBounds(this .Width -8,0,8,8);
btn3.SetBounds(0,(this .Height -8)/2,8,8);
btn4.SetBounds(this .Width -8,(this.Height -8)/2,8,8);
btn5.SetBounds(0,this.Height -8,8,8);
btn6.SetBounds((this .Width -8)/2,this .Height -8,8,8);
btn7.SetBounds(this .Width -8,this .Height -8,8,8);
}

int x0, y0;
private void btn0_MouseDown(object sender, MouseEventArgs e)
{
x0 = e.X; y0 = e.Y;
}

private void btn0_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x0; int dy = e.Y - y0;
this.SetBounds(this.Location.X + dx, this.Location.Y + dy, this.Width - dx, this.Height - dy);
}
}

int x1, y1;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
x1 = e.X; y1 = e.Y;
}

private void btn1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x1; int dy = e.Y - y1;
this.SetBounds(this .Location .X,this .Location .Y +dy,this.Width ,this.Height -dy);
}
}

int x2, y2;
private void btn2_MouseDown(object sender, MouseEventArgs e)
{
x2 = e.X; y2 = e.Y;
}

private void btn2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x2; int dy = e.Y - y2;
this.SetBounds(this .Location .X ,this .Location .Y+dy ,this .Width +dx,this .Height -dy);
}
}

int x3, y3;
private void btn3_MouseDown(object sender, MouseEventArgs e)
{
x3 = e.X; y3 = e.Y;
}

private void btn3_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x3; int dy = e.Y - y3;
this.SetBounds(this .Location .X+dx ,this .Location .Y,this .Width -dx,this .Height );
}
}

int x4, y4;
private void btn4_MouseDown(object sender, MouseEventArgs e)
{
x4 = e.X; y4 = e.Y;
}

private void btn4_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x4; int dy = e.Y - y4;
this.SetBounds(this .Location .X,this .Location .Y ,this .Width+dx,this .Height );
}
}

int x5, y5;
private void btn5_MouseDown(object sender, MouseEventArgs e)
{
x5 = e.X; y5 = e.Y;
}

private void btn5_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x5; int dy = e.Y - y5;
this.SetBounds(this .Location .X +dx,this .Location .Y ,this.Width -dx,this .Height +dy);
}
}

int x6, y6;
private void btn6_MouseDown(object sender, MouseEventArgs e)
{
x6 = e.X; y6 = e.Y;
}

private void btn6_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x6; int dy = e.Y - y6;
this.SetBounds(this .Location .X ,this .Location .Y ,this .Width,this .Height +dy);
}
}

int x7, y7;
private void btn7_MouseDown(object sender, MouseEventArgs e)
{
x7 = e.X; y7 = e.Y;
}

private void btn7_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - x7; int dy = e.Y - y7;
this.SetBounds(this .Location .X ,this.Location .Y ,this .Width +dx,this .Height +dy);
}
}
#endregion

}
}
Navymk 2008-05-26
  • 打赏
  • 举报
回复
晕,刚才我也是用picturebox模拟了这种效果...呵呵,我用c#,vb代码看得好累...但是透明还没实现...感觉gdi绘图似乎不是我想要的效果
wzuomin 2008-05-26
  • 打赏
  • 举报
回复
Navymk 2008-05-26
  • 打赏
  • 举报
回复
谢谢.
gdi的绘制已经了解了.
我现在需要的是一个可以拖动并改变大小的半透明的层.gdi绘制的这个矩形可以实现吗?
SuperTyro 2008-05-26
  • 打赏
  • 举报
回复
在GID+ 中 有画笔类

可以使用画笔来画 当然 这需要使用画笔的一些方法

这样 我给你一个例子吧

一个画椭圆的例子
protected override void OnPaint(PaintEventArgs e)
{
//创建对象
Graphics g=e.Graphics;
//创建一支宽度为8象素的实践笔
Pen solidPen=new Pen(Color.Black,8);
//创建一支虚线笔
Pen dashPen=new Pen(Color.Red);

dashPen.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;

//绘制一个椭圆:边框的宽度为80,高度为40,左上角位于(100,50)
g.DrawEllipse(solidPen,100,50,80,40);

//再来个绘制弧线的 我正好复习下代码 ^_^
g.DrawArc(dashPen,100,50,140,70,30,180);

//释放画笔
solidPen.Dispose();
dashPen.Dispose();
}

希望对你有帮助

我是小菜鸟! 说的不好清见谅, 希望能够帮到你!
Navymk 2008-05-26
  • 打赏
  • 举报
回复
具体说说思路可否?
SuperTyro 2008-05-26
  • 打赏
  • 举报
回复
你可以看看 有关GDI+编程的部分

他对于动态的图片处理 和类似画板功能的实现 都很有帮助的



我是小菜鸟! 说的不好清见谅, 希望能够帮到你!
财富实验室 2008-05-26
  • 打赏
  • 举报
回复
gdi+
Navymk 2008-05-26
  • 打赏
  • 举报
回复
补充一句:仅画出区域就可以了,不必裁切.

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧