/// <summary> /// Initializes a new instance of the <see cref="DataTransferredEventArgs" /> class. /// </summary> /// <param name="source">The source.</param> /// <param name="memoryInfo">The memory information.</param> /// <param name="memoryData">The memory data.</param> public DataTransferredEventArgs(DataSource source, TWImageMemXfer memoryInfo, byte[] memoryData) { TransferType = XferMech.Memory; DataSource = source; MemoryInfo = memoryInfo; MemoryData = memoryData; }
public static extern ReturnCode DsmWinNew( [In, Out] TWIdentity origin, [In, Out] TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, [In, Out] TWImageMemXfer data);
public static ReturnCode DsmEntry( TWIdentity origin, TWIdentity destination, Message msg, TWImageMemXfer data) { if (PlatformInfo.Current.IsWindows) { if (PlatformInfo.Current.UseNewWinDSM) { return(NativeMethods.DsmWinNew(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data)); } else { return(NativeMethods.DsmWinOld(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data)); } } else if (PlatformInfo.Current.IsLinux) { return(NativeMethods.DsmLinux(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data)); } throw new PlatformNotSupportedException(); }
/// <summary> /// This operation is used to initiate the transfer of an image from the Source to the application via /// the Memory-File transfer mechanism. /// </summary> /// <param name="xfer">The xfer.</param> /// <returns></returns> public ReturnCode Get(TWImageMemXfer xfer) { Session.VerifyState(6, 7, DataGroups.Image, DataArgumentType.ImageMemFileXfer, Message.Get); return(Dsm.DsmEntry(Session.AppId, Session.CurrentSource.Identity, Message.Get, xfer)); }
static void DoImageXferredEventRoutine(ITwainSessionInternal session, IntPtr dataPtr, TWImageMemXfer memInfo, byte[] memData, string filePath, FileFormat format) { DataTransferredEventArgs args = null; if (dataPtr != IntPtr.Zero) { args = new DataTransferredEventArgs(session.CurrentSource, dataPtr); } else if (memData != null) { args = new DataTransferredEventArgs(session.CurrentSource, memInfo, memData); } else { args = new DataTransferredEventArgs(session.CurrentSource, filePath, format); } session.SafeSyncableRaiseEvent(args); }
static ReturnCode DoImageMemoryFileXfer(ITwainSessionInternal session) { // since it's memory-file xfer need info from both (maybe) TWSetupMemXfer memInfo; TWSetupFileXfer fileInfo; ReturnCode xrc = ReturnCode.Failure; if (session.DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success && session.DGControl.SetupFileXfer.Get(out fileInfo) == ReturnCode.Success) { TWImageMemXfer xferInfo = new TWImageMemXfer(); var tempFile = Path.GetTempFileName(); string finalFile = null; try { // no strip or tile here, just chunks xferInfo.Memory = new TWMemory { Flags = MemoryFlags.AppOwns | MemoryFlags.Pointer, Length = memInfo.Preferred, TheMem = PlatformInfo.Current.MemoryManager.Allocate(memInfo.Preferred) }; xrc = ReturnCode.Success; using (var outStream = File.OpenWrite(tempFile)) { do { xrc = session.DGImage.ImageMemFileXfer.Get(xferInfo); if (xrc == ReturnCode.Success || xrc == ReturnCode.XferDone) { session.ChangeState(7, true); byte[] buffer = new byte[(int)xferInfo.BytesWritten]; IntPtr lockPtr = IntPtr.Zero; try { lockPtr = PlatformInfo.Current.MemoryManager.Lock(xferInfo.Memory.TheMem); Marshal.Copy(lockPtr, buffer, 0, buffer.Length); } finally { if (lockPtr != IntPtr.Zero) { PlatformInfo.Current.MemoryManager.Unlock(xferInfo.Memory.TheMem); //PlatformInfo.Current.MemoryManager.Unlock(lockPtr); } } outStream.Write(buffer, 0, buffer.Length); } } while (xrc == ReturnCode.Success); } if (xrc == ReturnCode.XferDone) { finalFile = fileInfo.ChangeExtensionByFormat(tempFile); File.Move(tempFile, finalFile); } else { HandleReturnCode(session, xrc); } } catch (Exception ex) { session.SafeSyncableRaiseEvent(new TransferErrorEventArgs(ex)); } finally { session.ChangeState(6, true); if (xferInfo.Memory.TheMem != IntPtr.Zero) { PlatformInfo.Current.MemoryManager.Free(xferInfo.Memory.TheMem); } if (File.Exists(tempFile)) { File.Delete(tempFile); } } if (File.Exists(finalFile)) { DoImageXferredEventRoutine(session, IntPtr.Zero, null, null, finalFile, fileInfo.Format); } } return(xrc); }
static ReturnCode DoImageMemoryXfer(ITwainSessionInternal session) { TWSetupMemXfer memInfo; ReturnCode xrc = session.DGControl.SetupMemXfer.Get(out memInfo); if (xrc == ReturnCode.Success) { TWImageMemXfer xferInfo = new TWImageMemXfer(); try { // how to tell if going to xfer in strip vs tile? // if tile don't allocate memory in app? xferInfo.Memory = new TWMemory { Flags = MemoryFlags.AppOwns | MemoryFlags.Pointer, Length = memInfo.Preferred, TheMem = PlatformInfo.Current.MemoryManager.Allocate(memInfo.Preferred) }; do { xrc = session.DGImage.ImageMemXfer.Get(xferInfo); if (xrc == ReturnCode.Success || xrc == ReturnCode.XferDone) { if (session.State != 7) { session.ChangeState(7, true); } // optimize and allocate buffer only once instead of inside the loop? byte[] buffer = new byte[(int)xferInfo.BytesWritten]; IntPtr lockPtr = IntPtr.Zero; try { lockPtr = PlatformInfo.Current.MemoryManager.Lock(xferInfo.Memory.TheMem); Marshal.Copy(lockPtr, buffer, 0, buffer.Length); } finally { if (lockPtr != IntPtr.Zero) { PlatformInfo.Current.MemoryManager.Unlock(xferInfo.Memory.TheMem); } } DoImageXferredEventRoutine(session, IntPtr.Zero, xferInfo, buffer, null, (FileFormat)0); } } while (xrc == ReturnCode.Success); HandleReturnCode(session, xrc); } catch (Exception ex) { session.SafeSyncableRaiseEvent(new TransferErrorEventArgs(ex)); } finally { session.ChangeState(6, true); if (xferInfo.Memory.TheMem != IntPtr.Zero) { PlatformInfo.Current.MemoryManager.Free(xferInfo.Memory.TheMem); } } } return(xrc); }