Home › Forums › TWAIN Classic › Saraff Twain (c#) memory transfer of tiff corrupted
- This topic has 0 replies, 1 voice, and was last updated 7 years, 5 months ago by ghostbust555.
Viewing 1 post (of 1 total)
- AuthorPosts
I am using code from the samples provided by Saraff twain to try and scan and get a tiff image. The image keeps coming out with data but corrupted. If anyone knows why these are coming out corrupted please let me know. I have it working with file transfers, but I need to use compression.
private void Twain_MemXferEvent(object sender, Twain32.MemXferEventArgs e)
{
try
{
if (TiffMemXfer.Current != null)
{
long _bitsPerRow = e.ImageInfo.BitsPerPixel * e.ImageMemXfer.Columns;
long _bytesPerRow = Math.Min(e.ImageMemXfer.BytesPerRow, (_bitsPerRow >> 3) + ((_bitsPerRow & 0x07) != 0 ? 1 : 0));
using (MemoryStream _stream = new MemoryStream())
{
for (int i = 0; i < e.ImageMemXfer.Rows; i++)
{
_stream.Position = 0;
_stream.Write(e.ImageMemXfer.ImageData, (int)(e.ImageMemXfer.BytesPerRow * i), (int)_bytesPerRow);
TiffMemXfer.Current.Strips.Add(TiffMemXfer.Current.Writer.WriteData(_stream.ToArray()));
TiffMemXfer.Current.StripByteCounts.Add((uint)_stream.Length);
}
}
}
else
{
MemFileXfer.Current.Writer.Write(e.ImageMemXfer.ImageData);
}
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("{0}: {1}n{2}", ex.GetType().Name, ex.Message, ex.StackTrace));
}
}
private void Twain_SetupMemXferEvent(object sender, Twain32.SetupMemXferEventArgs e)
{
try
{
if (this._twain.Capabilities.XferMech.GetCurrent() == TwSetupTransfer.Memory)
{
if (TiffMemXfer.Current == null)
{
TiffMemXfer.Create((int)e.BufferSize);
}
#region Color Map
if (e.ImageInfo.PixelType == TwPixelType.Palette)
{
Twain32.ColorPalette _palette = this._twain.Palette.Get();
TiffMemXfer.Current.ColorMap = new ushort[_palette.Colors.Length * 3];
for (int i = 0; i < _palette.Colors.Length; i++)
{
TiffMemXfer.Current.ColorMap = (ushort)(_palette.Colors.R);
TiffMemXfer.Current.ColorMap = (ushort)(_palette.Colors.G);
TiffMemXfer.Current.ColorMap = (ushort)(_palette.Colors.B);
}
}
#endregion
}
else
{ // MemFile
MemFileXfer.Create((int)e.BufferSize, this._twain.Capabilities.ImageFileFormat.GetCurrent().ToString().ToLower());
}
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("{0}: {1}n{2}", ex.GetType().Name, ex.Message, ex.StackTrace));
}
}
private void Twain_XferDone(object sender, Twain32.XferDoneEventArgs e)
{
if (TiffMemXfer.Current != null && TiffMemXfer.Current.Writer != null)
{
Twain32.ImageInfo _info = e.GetImageInfo();
Collection_tags = new Collection {
Tag.Create(TiffTags.ImageWidth,(uint)_info.ImageWidth),
Tag.Create(TiffTags.ImageLength,(uint)_info.ImageLength),
Tag.Create(TiffTags.SamplesPerPixel,(ushort)_info.BitsPerSample.Length),
Tag.Create(TiffTags.BitsPerSample,new Func (val=>{
ushort[] _result=new ushort[_info.BitsPerSample.Length];
for(int i=0; i<_result.Length; i++) {
_result=(ushort)_info.BitsPerSample;
}
return _result;
})(_info.BitsPerSample)),
Tag.Create(TiffTags.Orientation,(ushort)TiffOrientation.TOPLEFT),
Tag.Create(TiffTags.Compression,TiffCompression.NONE),
Tag.Create(TiffTags.ResolutionUnit,new Func (val=>{
switch(val){
case TwUnits.Centimeters:
return TiffResolutionUnit.CENTIMETER;
case TwUnits.Inches:
return TiffResolutionUnit.INCH;
default:
return TiffResolutionUnit.NONE;
}
})(this._twain.Capabilities.Units.GetCurrent())),
Tag.Create(TiffTags.XResolution,(1UL<<32)|(ulong)_info.XResolution),
Tag.Create(TiffTags.YResolution,(1UL<<32)|(ulong)_info.YResolution),
Tag.Create(TiffTags.StripOffsets,TiffMemXfer.Current.Strips.ToArray()),
Tag.Create(TiffTags.StripByteCounts,TiffMemXfer.Current.StripByteCounts.ToArray()),
Tag.Create(TiffTags.RowsPerStrip,1u),
//Tag.Create(TiffTags.Software, Application.ProductName.ToCharArray()),
Tag.Create(TiffTags.Model,this._twain.GetSourceProductName(this._twain.SourceIndex).ToCharArray()),
Tag.Create(TiffTags.DateTime,DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss").PadRight(20,' ').ToCharArray()),
Tag.Create(TiffTags.HostComputer,Environment.MachineName.ToCharArray()),
Tag.Create(TiffTags.PlanarConfiguration,(ushort)TiffPlanarConfig.CONTIG),
Tag.Create(TiffTags.Copyright,((AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute),false)[0]).Copyright.ToCharArray())
};
switch (_info.PixelType)
{
case TwPixelType.BW:
_tags.Add(Tag.Create(TiffTags.PhotometricInterpretation, TiffPhotoMetric.BlackIsZero));
break;
case TwPixelType.Gray:
_tags.Add(Tag.Create(TiffTags.PhotometricInterpretation, TiffPhotoMetric.BlackIsZero));
break;
case TwPixelType.Palette:
_tags.Add(Tag.Create(TiffTags.PhotometricInterpretation, TiffPhotoMetric.Palette));
_tags.Add(Tag.Create(TiffTags.ColorMap, TiffMemXfer.Current.ColorMap));
break;
case TwPixelType.RGB:
_tags.Add(Tag.Create(TiffTags.PhotometricInterpretation, TiffPhotoMetric.RGB));
break;
default:
break;
}
TiffMemXfer.Current.Handle = TiffMemXfer.Current.Writer.WriteImageFileDirectory(TiffMemXfer.Current.Handle, _tags);
TiffMemXfer.Current.Strips.Clear();
TiffMemXfer.Current.StripByteCounts.Clear();
}
}
private void Twain_AcquireCompleted(object sender, EventArgs e)
{
if (this._twain.ImageCount > 0)
{
this.CurrentBitmap = this._twain.GetImage(0) as Bitmap;
}
if (TiffMemXfer.Current != null)
{
try
{
TiffMemXfer.Current.Writer.BaseStream.Seek(0, SeekOrigin.Begin);
this.CurrentBitmap = Image.FromStream(TiffMemXfer.Current.Writer.BaseStream) as Bitmap;
}
finally
{
TiffMemXfer.Dispose();
}
}
FinalizeScan();
}
private void Twain_AcquireError(object sender, Twain32.AcquireErrorEventArgs e)
{
FinalizeScan();
if (e.Exception.ConditionCode != TwCC.Success)
{
_onError?.Invoke("Twain encountered an acquire error: " + e.Exception.ToString());
}
}
- AuthorPosts
Viewing 1 post (of 1 total)