如何获得picturebox里面的image的handle

fatboyyhc 2009-02-06 09:05:21
这几天正在写一个画线程序,要用到异或线,所以用到了GDI API 里面的setROP2函数,而该函数里面有一个intptr类型的参数,而我的线需要画到picturebox里面的image上,如果我能获得image的handle就可以代入那个参数里面了,无奈是.net中的image是没有handle属性,控件才有。请问我该怎么做才能把线画到picturebox的image上呢?
...全文
1282 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fatboyyhc 2009-02-09
  • 打赏
  • 举报
回复
@net5i:

你的方法我试过了,画是能画,但不是异或模式,
Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int hdc = (int)g.GetHdc();
Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 0);
drawer.DrawLine(0, 0, 100, 100);
g.ReleaseHdc();
this.pictureBox1.Image = bmp as Image;

即使我把
Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 6);
参数改成了6,画出来的不是异或线,是一条白色的线。
郁闷当中。。。。
scy251147 2009-02-08
  • 打赏
  • 举报
回复
up
fatboyyhc 2009-02-08
  • 打赏
  • 举报
回复
@net5i:

首先非常感谢你的热心帮助,
明天会公司试试,这几天为了这个问题很烦恼。
fatboyyhc 2009-02-07
  • 打赏
  • 举报
回复
@zgke
不行,我把intptr hdc=((Bitmap)pictureBox1.Image).GetHbitmap();
再实例化 Win32XORPenDrawer penDrawer=new Win32XORPenDrawer( hdc.toToInt32(),6);
debug的时候,下面的构造函数那里 res是等于0的,也就是GetDC(new IntPtr(hWnd))返回0

  /// <summary>
/// 传入创建DC的句柄和绘图模式
/// </summary>
/// <param name="hWnd"></param>
/// <param name="drawMode"></param>
public Win32XORPenDrawer(int hWnd, int drawMode)
{
//GetDC by hwnd
this.hWnd = hWnd;
IntPtr res = GetDC(new IntPtr(hWnd));
hDc = res.ToInt32();
SetROP2(res, new IntPtr(drawMode));
}
net5i 2009-02-07
  • 打赏
  • 举报
回复
to 搂主2楼:
应该是搂主代码的问题,这个类的构造函数改成下面的看看呢:
        public Win32XORPenDrawer(int hWnd, int drawMode)
{
this.hDc = hWnd;
SetROP2((IntPtr)hDc, new IntPtr(drawMode));
}


另外,调用这个类的过程应该是:
Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int hdc = (int)g.GetHdc();
Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 0);
drawer.DrawLine(0, 0, 100, 100);
g.ReleaseHdc();
this.pictureBox1.Image = bmp as Image;

这样应该没问题的,你可以试试
zgke 2009-02-07
  • 打赏
  • 举报
回复
((Bitmap)pictureBox1.Image).GetHbitmap();
zgke 2009-02-07
  • 打赏
  • 举报
回复
((Bitmap)pictureBox1.Image).GetHbitmap();
fatboyyhc 2009-02-07
  • 打赏
  • 举报
回复
我画线的API函数,是放到 pictureBox1_MouseClick()事件里面执行的。
fatboyyhc 2009-02-07
  • 打赏
  • 举报
回复
@net5i:
不过还是要谢谢你
fatboyyhc 2009-02-07
  • 打赏
  • 举报
回复
@net5i:

这种方法我试过了,不行啊

这个类就是用来画线的
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Win32APIDraw
{
/// <summary>
/// 封装交互线效果的类
/// </summary>
public class Win32XORPenDrawer : IDisposable
{
#region 声明Win32 api
struct POINTAPI
{
public int x;
public int y;
}

[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("gdi32.dll")]
private static extern bool MoveToEx(IntPtr hDC,
IntPtr x,
IntPtr y,
ref POINTAPI lpPoint);

[DllImport("gdi32.dll")]
private static extern IntPtr LineTo(IntPtr hdc, IntPtr x, IntPtr y);
[DllImport("gdi32.dll")]
private static extern IntPtr SetROP2(IntPtr hdc, IntPtr fnDrawMode);

#endregion

#region 构造函数
/// <summary>
/// 传入创建DC的句柄和绘图模式
/// </summary>
/// <param name="hWnd"></param>
/// <param name="drawMode"></param>
public Win32XORPenDrawer(int hWnd, int drawMode)
{
//GetDC by hwnd
this.hWnd = hWnd;
IntPtr res = GetDC(new IntPtr(hWnd));
hDc = res.ToInt32();
SetROP2(res, new IntPtr(drawMode));
}


~Win32XORPenDrawer()
{
Dispose(false);
}

#endregion

#region 私有成员
private int hDc;

private int hWnd;

private bool disposed = false;

private void Dispose(bool disposing)
{
if (!this.disposed)
{
//ReleaseDC
ReleaseDC(new IntPtr(hWnd), new IntPtr(hDc));
}
disposed = true;
}
#endregion

#region 公共接口
#region IDisposable 成员

public void Dispose()
{
Dispose(true);
}

#endregion

/// <summary>
/// 从x1, y1向x2, y2画线
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
public void DrawLine(int x1, int y1, int x2, int y2)
{
POINTAPI lpOld = new POINTAPI();
MoveToEx(new IntPtr(hDc), new IntPtr(x1), new IntPtr(y1), ref lpOld);
LineTo(new IntPtr(hDc), new IntPtr(x2), new IntPtr(y2));
}

public void MoveTo(int x, int y)
{
POINTAPI lpOld = new POINTAPI();
MoveToEx(new IntPtr(hDc), new IntPtr(x), new IntPtr(y), ref lpOld);
}

public void LineTo(int x, int y)
{
LineTo(new IntPtr(hDc), new IntPtr(x), new IntPtr(y));
}
#endregion
}


}
net5i 2009-02-06
  • 打赏
  • 举报
回复
创建Image的绘图对象就可以了:
Graphics g = Graphics.FromImage(pictureBox1.Image);
IntPtr hdc = g.GetHdc();
。。。调用你的API函数,参数使用hdc
g.ReleaseHdc();

111,130

社区成员

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

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

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