问题描述
我使用LeadTools进行扫描.
I use LeadTools for scanning.
我想将扫描图像转换为字节.
I want to convert scanning image to byte.
void twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
ScanImage = e.Image.Clone();
ImageSource source = RasterImageConverter.ConvertToSource(ScanImage, ConvertToSourceOptions.None);
}
如何将ImageSource转换为Byte数组?
How to convert ImageSource to Byte array?
推荐答案
除非明确需要ImageSource对象,否则无需转换为一个.您可以使用以下代码直接从Leadtools.RasterImage获取一个包含像素数据的字节数组:
Unless you explicitly need an ImageSource object, there's no need to convert to one. You can get a byte array containing the pixel data directly from Leadtools.RasterImage using this code:
int totalPixelBytes = e.Image.BytesPerLine * e.Image.Height;
byte[] byteArray = new byte[totalPixelBytes];
e.Image.GetRow(0, byteArray, 0, totalPixelBytes);
请注意,这仅给您原始像素数据.
Note that this gives you only the raw pixel data.
如果您需要一个包含完整图像(例如JPEG)的内存流或字节数组,则也无需转换为源.您可以像这样使用Leadtools.RasterCodecs类:
If you need a memory stream or byte array that contains a complete image such as JPEG, you also do not need to convert to source. You can use the Leadtools.RasterCodecs class like this:
RasterCodecs codecs = new RasterCodecs();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
codecs.Save(e.Image, memStream, RasterImageFormat.Jpeg, 24);
这篇关于如何将ImageSource转换为Byte数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!