编程语言
首页 > 编程语言> > C# && WPF

C# && WPF

作者:互联网

初学WPF和C#

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");
            }
        }
C#调用C++的dll
[DllImport("xxx.dll", CharSet = CharSet.Auto, CallingConvention = 、CallingConvention.Cdecl, EntryPoint = "xxx")]
        public static extern int xxx();
C#截图
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;
        }

标签:startX,startY,WIA,C#,endX,int,&&,WPF,endY
来源: https://blog.csdn.net/jtyylsphtz/article/details/118358648