Implemented memory-file transfer.

This commit is contained in:
soukoku 2014-04-07 19:21:33 -04:00
parent 05e4eb8504
commit 2820cf7a8d
9 changed files with 88 additions and 49 deletions

View File

@ -1,16 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Windows.Media.Imaging;
using NTwain.Properties;
using NTwain.Values;
using Microsoft.Win32.SafeHandles;
using NTwain.Properties;
using System;
namespace NTwain
{

View File

@ -46,11 +46,9 @@
<AssemblyOriginatorKeyFile>Sign.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncPump.cs" />

View File

@ -14,6 +14,6 @@ namespace NTwain
// keep this same in majors releases
public const string Release = "0.9.0.0";
// change this for each nuget release
public const string Build = "0.9.1";
public const string Build = "0.9.2";
}
}

View File

@ -869,11 +869,19 @@ namespace NTwain
private void DoImageMemoryXfer()
{
throw new NotImplementedException();
}
private void DoImageMemoryFileXfer()
{
// since it's memory-file xfer need info from both (maybe)
TWSetupMemXfer memInfo;
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success)
TWSetupFileXfer fileInfo;
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success &&
DGControl.SetupFileXfer.Get(out fileInfo) == ReturnCode.Success)
{
TWImageMemXfer xferInfo = new TWImageMemXfer();
var tempFile = Path.GetTempFileName();
string finalFile = null;
try
{
xferInfo.Memory = new TWMemory
@ -882,18 +890,75 @@ namespace NTwain
TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred)
};
var xrc = ReturnCode.Success;
do
using (var outStream = File.OpenWrite(tempFile))
{
xrc = DGImage.ImageMemXfer.Get(xferInfo);
if (xrc == ReturnCode.XferDone)
do
{
xrc = DGImage.ImageMemFileXfer.Get(xferInfo);
if (xrc == ReturnCode.Success ||
xrc == ReturnCode.XferDone)
{
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
Marshal.Copy(xferInfo.Memory.TheMem, buffer, 0, buffer.Length);
outStream.Write(buffer, 0, buffer.Length);
}
} while (xrc == ReturnCode.Success);
}
if (xrc == ReturnCode.XferDone)
{
switch (fileInfo.Format)
{
case FileFormat.Bmp:
finalFile = Path.ChangeExtension(tempFile, ".bmp");
break;
case FileFormat.Dejavu:
finalFile = Path.ChangeExtension(tempFile, ".dejavu");
break;
case FileFormat.Exif:
finalFile = Path.ChangeExtension(tempFile, ".exit");
break;
case FileFormat.Fpx:
finalFile = Path.ChangeExtension(tempFile, ".fpx");
break;
case FileFormat.Jfif:
finalFile = Path.ChangeExtension(tempFile, ".jpg");
break;
case FileFormat.Jp2:
finalFile = Path.ChangeExtension(tempFile, ".jp2");
break;
case FileFormat.Jpx:
finalFile = Path.ChangeExtension(tempFile, ".jpx");
break;
case FileFormat.Pdf:
case FileFormat.PdfA:
case FileFormat.PdfA2:
finalFile = Path.ChangeExtension(tempFile, ".pdf");
break;
case FileFormat.Pict:
finalFile = Path.ChangeExtension(tempFile, ".pict");
break;
case FileFormat.Png:
finalFile = Path.ChangeExtension(tempFile, ".png");
break;
case FileFormat.Spiff:
finalFile = Path.ChangeExtension(tempFile, ".spiff");
break;
case FileFormat.Tiff:
case FileFormat.TiffMulti:
finalFile = Path.ChangeExtension(tempFile, ".tif");
break;
case FileFormat.Xbm:
finalFile = Path.ChangeExtension(tempFile, ".xbm");
break;
default:
finalFile = Path.ChangeExtension(tempFile, ".unknown");
break;
}
} while (xrc == ReturnCode.Success);
File.Move(tempFile, finalFile);
}
}
finally
{
@ -901,16 +966,19 @@ namespace NTwain
{
MemoryManager.Instance.Free(xferInfo.Memory.TheMem);
}
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
if (File.Exists(finalFile))
{
OnDataTransferred(new DataTransferredEventArgs(IntPtr.Zero, finalFile));
}
}
}
private void DoImageMemoryFileXfer()
{
// no way to test, not supported by sample source
throw new NotImplementedException();
}
#endregion
#endregion

View File

@ -146,8 +146,8 @@ namespace NTwain
{
if (tryUpperWord)
{
// small routine to see if works with bad sources that put
// 16bit value in the upper word instead of lower word.
// small routine to work with bad sources that put
// 16bit value in the upper word instead of lower word as per the twain spec.
var rawType = Enum.GetUnderlyingType(returnType);
if (typeof(ushort).IsAssignableFrom(rawType))
{

View File

@ -1,17 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NTwain.Triplets;
using NTwain.Data;
using NTwain.Values;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Permissions;
using System.IO;
using System.ComponentModel;
using System.Threading;
namespace NTwain
{

View File

@ -1,10 +1,6 @@
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
namespace NTwain
{

View File

@ -1,9 +1,5 @@
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
namespace NTwain

View File

@ -17,7 +17,7 @@
<Button Content="Test Acquire" Click="Button_Click_1"></Button>
</StackPanel>
<ListBox x:Name="CapList" Grid.Row="1" BorderThickness="0"></ListBox>
<ListBox x:Name="CapList" Grid.Row="1" BorderThickness="0" ScrollViewer.CanContentScroll="False"></ListBox>
<modern:AnimatedScrollViewer Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">