初学WPF和C#
private void scanner_click(object sender, RoutedEventArgs e) { ImageFile imageFile = null; CommonDialogClass cdc = new WIA.CommonDialogClass(); try { imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.TextIntent, WIA.WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false); } catch (System.Runtime.InteropServices.COMException) { imageFile = null; } if (imageFile != null) { imageFile.SaveFile(@"E:\temp_test.bmp"); } }
[DllImport("xxx.dll", CharSet = CharSet.Auto, CallingConvention = 、CallingConvention.Cdecl, EntryPoint = "xxx")] public static extern int xxx();
public static Bitmap cutImage(Bitmap bmp) { int startX = 1520; int startY = 1776; int endX = 1736; int endY = 2078;//这些数据时我瞎测试的 if (startX == endX || startY == endY) { return null; //图片的宽度和高度一定都是大于0的整数 } if (startX > endX) //一定要保证startX < endX { int temp = startX; startX = endX; endX = temp; } if (startY > endY) //一定要保证startY < endY { int temp = startY; startY = endY; endY = temp; } //获得新图片的宽度和高度 int width = endX - startX; int height = endY - startY; //根据截图的宽度和高度新建图片 Bitmap bmp2 = new Bitmap(width, height); //创建作图区域 Graphics g = Graphics.FromImage(bmp2); //截取原图相应区域写入作图区 g.DrawImage(bmp, 0, 0, new System.Drawing.Rectangle(startX, startY, width, height), GraphicsUnit.Pixel); return bmp2; }