using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mshtml;
using System.Drawing.Imaging;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport(@"Regn.dll")]
public static extern void GetCode(string filename, StringBuilder code);
private void button1_Click(object sender, EventArgs e)
{
HTMLDocument html = (HTMLDocument)this.webBrowser1.Document.DomDocument;//下面代码中,获取图片的方式有很多,因为比较简单,我就不列举了,直接用ID来做为例子的
IHTMLControlElement img = (IHTMLControlElement)webBrowser1.Document.Images["pic2"].DomElement;
IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange();
range.add(img);
range.execCommand("Copy", false, null);
img = null;
range = null;
if (Clipboard.ContainsImage())
{
Clipboard.GetImage().Save(Application.StartupPath + "//code.bmp");
int width = 40;
int height = 20;
int offsetX = 0;
int offsetY = 0;
Bitmap sourceBitmap = new Bitmap(Application.StartupPath + "//code.bmp");
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);
}
sourceBitmap.Dispose();
resultBitmap.Save(Application.StartupPath + "//code.bmp", ImageFormat.Bmp);
string filename = Application.StartupPath + "//code.bmp";
StringBuilder code = new StringBuilder(1024);
GetCode(filename, code);
MessageBox.Show(code.ToString());
}
else
{ MessageBox.Show("执行不成功"); }
Clipboard.Clear();
}
private bool ThumbnailCallback()
{
return false;
}
}
}