领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
 
namespace Bro.Common.ImageCanvas
{
    public static class StaticHelper
    {
        static BitmapPalette palette8Index = null;
        static BitmapPalette Palette8Index
        {
            get
            {
                if (palette8Index == null)
                {
                    List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
                    for (int i = 0; i < 256; i++)
                    {
                        colors.Add(System.Windows.Media.Color.FromRgb((byte)i, (byte)i, (byte)i));
                    }
                    palette8Index = new BitmapPalette(colors);
                }
 
                return palette8Index;
            }
        }
 
        [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
 
        public static WriteableBitmap BitmapToWriteableBitmap(this Bitmap image)
        {
            BitmapPalette palette = null;
            System.Windows.Media.PixelFormat format = PixelFormats.Bgr32;
            switch (image.PixelFormat)
            {
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                    format = PixelFormats.Bgr32;
                    break;
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    format = PixelFormats.Bgr24;
                    break;
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                    format = PixelFormats.Indexed8;
                    //List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
                    //for (int i = 0; i < 256; i++)
                    //{
                    //    colors.Add(System.Windows.Media.Color.FromRgb((byte)i, (byte)i, (byte)i));
                    //}
                    //palette = new BitmapPalette(colors);
                    palette = Palette8Index;
                    break;
            }
 
            WriteableBitmap wbmp = new WriteableBitmap(image.Width, image.Height, 96, 96, format, palette);
 
            var bytes = (uint)wbmp.PixelWidth * (uint)wbmp.PixelHeight * (uint)wbmp.Format.BitsPerPixel / (uint)8;
 
            var rBitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadOnly, image.PixelFormat);
 
            wbmp.Lock();
            unsafe
            {
                //Buffer.MemoryCopy(rBitmapData.Scan0.ToPointer(), wbmp.BackBuffer.ToPointer(), bytes, bytes);
                CopyMemory(wbmp.BackBuffer, rBitmapData.Scan0, bytes);
                //byte[] rawData = new byte[bytes];
                //Marshal.Copy(rBitmapData.Scan0, rawData, 0, bytes);
                //Marshal.Copy(rawData, 0, wbmp.BackBuffer, rawData.Length);
            }
            wbmp.AddDirtyRect(new Int32Rect(0, 0, image.Width, image.Height));
            wbmp.Unlock();
 
            image.UnlockBits(rBitmapData);
 
            return wbmp;
        }
 
        [DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);
        public static ImageSource BitmapToImageSource(this Bitmap image)
        {
            IntPtr myImagePtr = image.GetHbitmap();     //创建GDI对象,返回指针
 
            BitmapSource imgsource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(myImagePtr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());  //创建imgSource
 
            DeleteObject(myImagePtr);
 
            return imgsource;
        }
    }
}