How to convert ImageSource to Byte array?

后端 未结 6 593
眼角桃花
眼角桃花 2020-11-30 14:20

I use LeadTools for scanning.

I want to convert scanning image to byte.

void twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
 {
         


        
6条回答
  •  死守一世寂寞
    2020-11-30 15:10

    I use this class to work with Image in WPF

    public static class ImageHelper
        {
            /// 
            /// ImageSource to bytes
            /// 
            /// 
            /// 
            /// 
            public static byte[] ImageSourceToBytes(BitmapEncoder encoder, ImageSource imageSource)
            {
                byte[] bytes = null;
                var bitmapSource = imageSource as BitmapSource;
    
                if (bitmapSource != null)
                {
                    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
    
                    using (var stream = new MemoryStream())
                    {
                        encoder.Save(stream);
                        bytes = stream.ToArray();
                    }
                }
    
                return bytes;
            }
    
            [DllImport("gdi32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool DeleteObject(IntPtr value);
    
            public static BitmapSource GetImageStream(Image myImage)
            {
                var bitmap = new Bitmap(myImage);
                IntPtr bmpPt = bitmap.GetHbitmap();
                BitmapSource bitmapSource =
                 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                       bmpPt,
                       IntPtr.Zero,
                       Int32Rect.Empty,
                       BitmapSizeOptions.FromEmptyOptions());
    
                //freeze bitmapSource and clear memory to avoid memory leaks
                bitmapSource.Freeze();
                DeleteObject(bmpPt);
    
                return bitmapSource;
            }
    
            /// 
            /// Convert String to ImageFormat
            /// 
            /// 
            /// 
            public static System.Drawing.Imaging.ImageFormat ImageFormatFromString(string format)
            {
                if (format.Equals("Jpg"))
                    format = "Jpeg";
                Type type = typeof(System.Drawing.Imaging.ImageFormat);
                BindingFlags flags = BindingFlags.GetProperty;
                object o = type.InvokeMember(format, flags, null, type, null);
                return (System.Drawing.Imaging.ImageFormat)o;
            }
    
            /// 
            /// Read image from path
            /// 
            /// 
            /// 
            /// 
            public static byte[] BytesFromImage(String imageFile, System.Drawing.Imaging.ImageFormat imageFormat)
            {
                MemoryStream ms = new MemoryStream();
                Image img = Image.FromFile(imageFile);
                img.Save(ms, imageFormat);
                return ms.ToArray();
            }
    
            /// 
            /// Convert image to byte array
            /// 
            /// 
            /// 
            /// 
            public static byte[] ImageToByteArray(System.Drawing.Image imageIn, System.Drawing.Imaging.ImageFormat imageFormat)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, imageFormat);
                return ms.ToArray();
            }
    
            /// 
            /// Byte array to photo
            /// 
            /// 
            /// 
            public static Image ByteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
        }
    

    So try different approaches and modernize this class as you need.

提交回复
热议问题