| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6225 人关注过本帖
标题:谁知道c#中如何实现图片剪切功能
只看楼主 加入收藏
limeiping
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2007-1-31
结帖率:66.67%
收藏
 问题点数:0 回复次数:20 
谁知道c#中如何实现图片剪切功能

有那位知道如何实现这种功能:在图片上显示一个方框,可以试方框的大小,双击方框就把方框以内的内容剪切下来,就像PHOTOSHOP中的剪切功能。

搜索更多相关主题的帖子: 功能 剪切 
2007-02-28 20:30
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 

这到不难,我有实现过,代码是有一点,有兴趣加入群,里面有源码。有源码这里就不罗列了。


XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-03-01 10:38
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
蓝冰:你的群号多少?

2007-03-01 11:29
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
我的签名

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-03-01 11:59
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 

找了段代码你看看:
private void button1_Click(object sender, EventArgs e)
{
FormCapture cf = new FormCapture();
cf.ShowDialog();
if (cf.Image != null)
{
System.IO.MemoryStream Ms = new System.IO.MemoryStream();
cf.Image.Save(Ms, System.Drawing.Imaging.ImageFormat.Gif);

MyPicture pic = new MyPicture();
pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
pic.BackColor = this.RTBSend.BackColor;
pic.Image = System.Drawing.Image.FromStream(Ms);
Ms.Close();
System.Random R = new Random();
pic.Tag = R.Next(200, 2147483627);

System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(Application.StartupPath + "\\CapImage");
if (!dInfo.Exists)
dInfo.Create();

cf.Image.Save(Application.StartupPath + "\\CapImage\\" + pic.Tag.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
}
cf.Dispose();
}


2007-03-01 13:47
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace ScreenCap
{
/// <summary>
/// MyPicture 的摘要说明。
/// </summary>
public class MyPicture : System.Windows.Forms.PictureBox
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public MyPicture()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// this.SizeMode=System.Windows.Forms.PictureBoxSizeMode.AutoSize;
// TODO: 在 InitializeComponent 调用后添加任何初始化

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

private string filename="";//GIF图片文件的绝对路径
public string FileName
{
set{filename=value;}
get{return filename;}
}

public bool IsSent=false;
public void playGif()
{
System.Drawing.ImageAnimator.Animate(this.Image,new System.EventHandler(this.OnFrameChanged));
}

private void OnFrameChanged(object sender, EventArgs e)
{

//Force a call to the Paint event handler.
this.Invalidate();
//this.Refresh();
}

}
}


2007-03-01 13:48
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ScreenCap
{
/// <summary>
/// CaptureForm 的摘要说明。
/// </summary>
public class FormCapture : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

Point op;
Rectangle area=Rectangle.Empty;
private Image img;
public FormCapture()
{
this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint,true);
InitializeComponent();
this.Bounds=System.Windows.Forms.Screen.PrimaryScreen.Bounds;

this.BackgroundImage=this.GetDestopImage();
}

public Image Image
{
get{return this.img;}
}


/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private Image GetDestopImage()
{
Rectangle area=System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap bm=new Bitmap(area.Width,area.Height);
Graphics g=Graphics.FromImage(bm);

System.IntPtr p=g.GetHdc();
IntPtr c=GetDesktopWindow();
System.IntPtr ddc=GetDC(c);
API.BitBlt(p,0,0,this.Width,this.Height,ddc,0,0,API.SRCCOPY);
API.ReleaseDC(c,ddc);
g.ReleaseHdc(p);
return bm;
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// CaptureForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "CaptureForm";
this.Text = "CaptureForm";
this.TopMost = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
//this.Click += new System.EventHandler(this.CaptureForm_Click);
this.DoubleClick += new System.EventHandler(this.CaptureForm_DoubleClick);

}
#endregion

[DllImport("user32.dll",EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetDesktopWindow();

private void CaptureForm_DoubleClick(object sender, System.EventArgs e)
{
if(this.area.Width<=0 ||this.area.Height<=0)
{
this.DialogResult=DialogResult.Cancel;return;
}
Bitmap bm=new Bitmap(this.area.Width,this.area.Height);
Graphics g=Graphics.FromImage(bm);
g.DrawImage(this.BackgroundImage,0,0,this.area,GraphicsUnit.Pixel);
this.img=bm;
// Bitmap bm=new Bitmap(this.BackgroundImage);
// this.img=bm.Clone(this.area,System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
this.DialogResult=DialogResult.OK;
}

int index=-1;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown (e);
if(this.area==Rectangle.Empty && e.Button==MouseButtons.Left)
{
this.area.Location=new Point(e.X,e.Y);
}
this.op=new Point(e.X,e.Y);
this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
this.SetCursor();
}

private int GetSelectedHandle(Point p)
{
int index=-1;
for(int i=1;i<9;i++)
{
if(GetHandleRect(i).Contains(p))
{
index=i;
break;
}
}
if(this.area.Contains(p))index=0;
System.Diagnostics.Trace.WriteLine(area.ToString());
System.Diagnostics.Trace.WriteLine(p.ToString());
System.Diagnostics.Trace.WriteLine(index.ToString());
return index;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove (e);
if(this.Capture)
{
this.MoveHandleTo(new Point(e.X,e.Y));
this.Invalidate();
}
else
{
this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
this.SetCursor();
}
}
private void MoveHandleTo(Point point)
{
int left = area.Left;
int top = area.Top;
int right = area.Right;
int bottom = area.Bottom;

switch ( index )
{
case 0:
area.X+=point.X-op.X;
area.Y+=point.Y-op.Y;
this.op=point;
return;
case 1:
left = point.X;
top = point.Y;
break;
case 2:
top = point.Y;
break;
case 3:
right = point.X;
top = point.Y;
break;
case 4:
right = point.X;
break;
case 5:
right = point.X;
bottom = point.Y;
break;
case 6:
bottom = point.Y;
break;
case 7:
left = point.X;
bottom = point.Y;
break;
case 8:
left = point.X;
break;
}
this.op=point;
area.X=left;
area.Y=top;
area.Width=right-left;
area.Height=bottom-top;
}
private void SetCursor()
{
Cursor cr=Cursors.Default;
if(index==1 ||index==5)
{
cr=Cursors.SizeNWSE;
}
else if(index==2 ||index==6)
{
cr=Cursors.SizeNS;
}
else if(index==3 ||index==7)
{
cr=Cursors.SizeNESW;
}
else if(index==4 ||index==8)
{
cr=Cursors.SizeWE;
}
else if(index==0)
{
cr=Cursors.SizeAll;
}
Cursor.Current=cr;
}

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp (e);
int left = area.Left;
int top = area.Top;
int right = area.Right;
int bottom = area.Bottom;
area.X=Math.Min(left,right);
area.Y=Math.Min(top,bottom);
area.Width=Math.Abs(left-right);
area.Height=Math.Abs(top-bottom);
if(e.Button==MouseButtons.Right)
{
if(this.area==Rectangle.Empty)
{
this.DialogResult=DialogResult.Cancel;
}
else
{
this.area=Rectangle.Empty;
this.Invalidate();
}
}
this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
this.SetCursor();
}

protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint (e);
base.OnPaint(e);
e.Graphics.DrawRectangle(new Pen(this.ForeColor),this.area);

for(int i=1;i<9;i++)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red),this.GetHandleRect(i));
}
}
private Rectangle GetHandleRect(int index)
{
Point point = GetHandle(index);
return new Rectangle(point.X - 3, point.Y - 3, 7, 7);
}
private Point GetHandle(int index)
{
int x, y, xCenter, yCenter;

xCenter = area.X + area.Width/2;
yCenter = area.Y + area.Height/2;
x = area.X;
y = area.Y;

switch ( index )
{
case 1:
x = area.X;
y = area.Y;
break;
case 2:
x = xCenter;
y = area.Y;
break;
case 3:
x = area.Right;
y = area.Y;
break;
case 4:
x = area.Right;
y = yCenter;
break;
case 5:
x = area.Right;
y = area.Bottom;
break;
case 6:
x = xCenter;
y = area.Bottom;
break;
case 7:
x = area.X;
y = area.Bottom;
break;
case 8:
x = area.X;
y = yCenter;
break;
}

return new Point(x, y);

}

}
}


2007-03-01 13:49
limeiping
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2007-1-31
收藏
得分:0 

谢了,我去试试

2007-03-01 20:08
limeiping
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2007-1-31
收藏
得分:0 

提示上下文不存在名为"API",错误行API.BitBlt(p, 0, 0, this.Width, this.Height, ddc, 0, 0, API.SRCCOPY);

2007-03-02 08:23
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
你用到了BITLBLT它是API函数,不用它也可以用GDI+DRAWIMAGE实现!

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-03-02 08:30
快速回复:谁知道c#中如何实现图片剪切功能
数据加载中...
 
   



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

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