如何将ImageSource转换为Byte数组?
我使用LeadTools进行扫描。
我想将扫描图像转换为字节。
void twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e) { ScanImage = e.Image.Clone(); ImageSource source = RasterImageConverter.ConvertToSource(ScanImage, ConvertToSourceOptions.None); }
如何将ImageSource转换为Byte数组?
除非您明确需要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);
您似乎可以将.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像素
我使用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(); }
我使用这个类在WPF中使用Image
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; } }
因此,请尝试不同的方法,并根据需要对此类进行现代化。
上述就是C#学习教程:如何将ImageSource转换为Byte数组?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1017116.html