Come convertire ImageSource in array Byte?

Uso LeadTools per la scansione.

Voglio convertire l’immagine di scansione in byte.

void twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e) { ScanImage = e.Image.Clone(); ImageSource source = RasterImageConverter.ConvertToSource(ScanImage, ConvertToSourceOptions.None); } 

Come convertire ImageSource in array Byte?

A meno che non sia necessario un object ImageSource esplicitamente, non è necessario convertirlo in uno. È ansible ottenere un array di byte contenente i dati dei pixel direttamente da Leadtools.RasterImage utilizzando questo codice:

 int totalPixelBytes = e.Image.BytesPerLine * e.Image.Height; byte[] byteArray = new byte[totalPixelBytes]; e.Image.GetRow(0, byteArray, 0, totalPixelBytes); 

Nota che questo ti dà solo i dati grezzi dei pixel.

Se hai bisogno di un stream di memoria o di un array di byte che contenga un’immagine completa come JPEG, non devi nemmeno convertire in sorgente. Puoi usare la class Leadtools.RasterCodecs in questo modo:

 RasterCodecs codecs = new RasterCodecs(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); codecs.Save(e.Image, memStream, RasterImageFormat.Jpeg, 24); 

Sembra che tu possa trasmettere il risultato da .ConvertToSource a BitmapSource invece che a ImageSource.

Non sono sicuro al 100% come questo si traduca nel tuo caso, ma il documento di LeadTools mostra questo esempio VB:

  Dim source As BitmapSource Using raster As RasterImage = RasterImageConverter.ConvertFromSource(bitmap, ConvertFromSourceOptions.None) Console.WriteLine("Converted to RasterImage, bits/pixel is {0} and order is {1}", raster.BitsPerPixel, raster.Order) ' Perform image processing on the raster image using LEADTOOLS Dim cmd As New FlipCommand(False) cmd.Run(raster) ' Convert the image back to WPF using default options source = DirectCast(RasterImageConverter.ConvertToSource(raster, ConvertToSourceOptions.None), BitmapSource) End Using 

Penso che dovrebbe essere come

 BitmapSource source = RasterImageConverter.ConvertToSource(ScanImage, ConvertToSourceOptions.None) as BitmapSource; 

Quindi puoi copiare i pixel BitmapSource con BitmapSource.CopyPixels

Io uso un MemoryStream:

 var source = RasterImageConverter.ConvertToSource(ScanImage, ConvertToSourceOptions.None) as BitmapSource; byte[] data; JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source)); using (MemoryStream ms = new MemoryStream()) { encoder.Save(ms); data = ms.ToArray(); } 

Uso questa class per lavorare con 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; } } 

Quindi prova diversi approcci e ammoderna questa class di cui hai bisogno.