如何将ImageSource转换为Byte数组?

问题描述 投票:0回答:7

我使用 LeadTools 进行扫描。

我想将扫描图像转换为字节。

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

如何将ImageSource转换为Byte数组?

c# wpf leadtools-sdk
7个回答
7
投票

如果您使用 Xamarin,您可以使用这个:

public async Task<byte[]> ConvertImageSourceToBytesAsync(ImageSource imageSource)
{
    Stream stream = await ((StreamImageSource)imageSource).Stream(CancellationToken.None);
    byte[] bytesAvailable = new byte[stream.Length];
    stream.Read(bytesAvailable, 0, bytesAvailable.Length);

    return bytesAvailable;
}

1
投票

除非您明确需要 ImageSource 对象,否则无需转换为 ImageSource 对象。您可以使用以下代码直接从 Leadtools.RasterImage 获取包含像素数据的字节数组:

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

请注意,这只会为您提供原始像素数据。

如果您需要包含完整图像(例如 JPEG)的内存流或字节数组,则也不需要转换为源。您可以像这样使用 Leadtools.RasterCodecs 类:

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

1
投票

我在 Xamarin.Forms 中遇到了这个问题,我需要将相机拍摄的照片转换为字节数组。经过几天的尝试找出方法后,我在 Xamarin 论坛中看到了这个解决方案。

var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });

byte[] imageArray = null;

using (MemoryStream memory = new MemoryStream()) {

    Stream stream = photo.GetStream();
    stream.CopyTo(memory);
    imageArray = memory.ToArray();
}


来源:https://forums.xamarin.com/discussion/156236/how-to-get-the-bytes-from-the-imagesource-in-xamarin-forms


0
投票

看来您可以将 .ConvertToSource 的结果转换为 BitmapSource 而不是 ImageSource。

我不是 100% 确定这如何转化为您的案例,但 LeadTools 的文档显示了此 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

我觉得应该是这样的

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

然后您可以使用 BitmapSource.CopyPixels

复制 BitmapSource 像素

0
投票

我使用 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();
}

0
投票

我在 WPF 中使用此类来处理

Image

public static class ImageHelper
    {
        /// <summary>
        /// ImageSource to bytes
        /// </summary>
        /// <param name="encoder"></param>
        /// <param name="imageSource"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Convert String to ImageFormat
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Read image from path
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="imageFormat"></param>
        /// <returns></returns>
        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();
        }

        /// <summary>
        /// Convert image to byte array
        /// </summary>
        /// <param name="imageIn"></param>
        /// <param name="imageFormat"></param>
        /// <returns></returns>
        public static byte[] ImageToByteArray(System.Drawing.Image imageIn, System.Drawing.Imaging.ImageFormat imageFormat)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, imageFormat);
            return ms.ToArray();
        }

        /// <summary>
        /// Byte array to photo
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <returns></returns>
        public static Image ByteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    }

因此,尝试不同的方法并根据需要使此类现代化。


0
投票

改进 Led Machine 的答案,因为我的 ImageSource 来自未实现长度的 http 流

        public async Task<byte[]> ConvertImageSourceToBytesAsync(ImageSource imageSource)
        {
            using MemoryStream memoryStream = new();
            try
            {
                using Stream stream = await ((StreamImageSource)imageSource).Stream(CancellationToken.None);
                await stream.CopyToAsync(memoryStream);
                stream?.Dispose();
                return memoryStream.ToArray();
            }
            catch
            {
                return [];
            }
            finally
            {
                memoryStream?.Dispose();
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.