示例#1
0
 protected virtual void Dispose(bool disposing)
 {
     if (_handle != IntPtr.Zero)
     {
         Kernel32Native.GlobalFree(_handle);
     }
 }
示例#2
0
 public void Dispose()
 {
     if (!IsZero)
     {
         Kernel32Native.GlobalDeleteAtom(Id);
         Id = 0;
     }
 }
            /// <summary>
            /// Reads the value from a <see cref="TwainCapability" /> that was returned
            /// from a TWAIN source.
            /// </summary>
            /// <param name="capability">The capability.</param>
            /// <returns></returns>
            /// <exception cref="System.ArgumentNullException">
            /// capability
            /// or
            /// memoryManager
            /// </exception>
            /// <exception cref="System.ArgumentException">capability</exception>
            public static CapabilityReader ReadValue(TwainCapability capability)
            {
                if (capability == null)
                {
                    throw new ArgumentNullException("capability");
                }

                if (capability.Handle != IntPtr.Zero)
                {
                    IntPtr baseAddr = IntPtr.Zero;
                    try
                    {
                        baseAddr = Kernel32Native.GlobalLock(capability.Handle);
                        switch (capability.ContainerType)
                        {
                        case ContainerType.Array:
                            return(new CapabilityReader
                            {
                                ContainerType = capability.ContainerType,
                            }.ReadArrayValue(baseAddr));

                        case ContainerType.Enum:
                            return(new CapabilityReader
                            {
                                ContainerType = capability.ContainerType,
                            }.ReadEnumValue(baseAddr));

                        case ContainerType.One:
                            return(new CapabilityReader
                            {
                                ContainerType = capability.ContainerType,
                            }.ReadOneValue(baseAddr));

                        case ContainerType.Range:
                            return(new CapabilityReader
                            {
                                ContainerType = capability.ContainerType,
                            }.ReadRangeValue(baseAddr));

                        default:
                            throw new ArgumentException("Capability has bad format");
                        }
                    }
                    finally
                    {
                        if (baseAddr != IntPtr.Zero)
                        {
                            //memoryManager.Unlock(baseAddr);
                            Kernel32Native.GlobalUnlock(capability.Handle);
                        }
                    }
                }

                return(new CapabilityReader());
            }
示例#4
0
        public GlobalAtom(string name)
        {
            var id = Kernel32Native.GlobalAddAtom(name);

            if (id == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            Name = name;
            Id   = id;
        }
示例#5
0
        public void ReadBackValue()
        {
            IntPtr p = Kernel32Native.GlobalLock(_handle);

            try
            {
                Marshal.PtrToStructure(p, _value);
            }
            finally
            {
                Kernel32Native.GlobalUnlock(_handle);
            }
        }
        public Boolean Terminate()
        {
            var res = false;

            if (isLoaded)
            {
                res = Kernel32Native.TerminateProcess(pi.hProcess, 0);
            }

            if (res)
            {
                Console.WriteLine("Terminando proceso");
            }

            return(res);
        }
示例#7
0
        protected TwainCapability(Capabilities capabilities, ContainerType containerType, object value)
        {
            _capabilities  = capabilities;
            _containerType = containerType;
            _value         = value;

            _handle = Kernel32Native.GlobalAlloc(GlobalAllocFlags.Handle, Marshal.SizeOf(value));

            IntPtr p = Kernel32Native.GlobalLock(_handle);

            try
            {
                Marshal.StructureToPtr(value, p, false);
            }
            finally
            {
                Kernel32Native.GlobalUnlock(_handle);
            }
        }
        protected void TransferPicturesIncremental()
        {
            // see http://www.twain.org/wp-content/uploads/2017/03/TWAIN-2.4-Specification.pdf
            // page 4-20
            Console.WriteLine("TransferPicturesIncremental...");
            Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, "TransferPicturesIncremental...");

            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            PendingXfers pendingTransfer = new PendingXfers();
            TwainResult  result;

            try
            {
                int recievedBlockCount = 1;
                do
                {
                    pendingTransfer.Count = 0;     // the Twain source will fill this in during DsPendingTransfer

                    Console.WriteLine("Get the image info...");
                    // Get the image info
                    ImageInfo imageInfo = new ImageInfo();
                    result = Twain32Native.DsImageInfo(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Image,
                        DataArgumentType.ImageInfo,
                        Message.Get,
                        imageInfo);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }

                    /*Console.WriteLine("Get the image layout...");
                     * ImageLayout imageLayout = new ImageLayout();
                     * result = Twain32Native.DsImageLayout(
                     *  ApplicationId,
                     *  DataSource.SourceId,
                     *  DataGroup.Image,
                     *  DataArgumentType.ImageLayout,
                     *  Message.GetCurrent,
                     *  imageLayout);
                     *
                     * if (result != TwainResult.Success)
                     * {
                     *  DataSource.Close();
                     *  break;
                     * }*/

                    // Setup Destination Bitmap
                    Bitmap bitmap = BitmapRenderer.NewBitmapForImageInfo(imageInfo);

                    Console.WriteLine("Setup incremental Memory XFer...");
                    // Setup incremental Memory XFer
                    SetupMemXfer setupMemXfer = new SetupMemXfer();
                    result = Twain32Native.DsSetupMemXfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Control,
                        DataArgumentType.SetupMemXfer,
                        Message.Get,
                        setupMemXfer
                        );

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }

                    Console.WriteLine("allocate the preferred buffer size...");
                    // allocate the preferred buffer size
                    // see twain spec pdf, page 4-21
                    ImageMemXfer imageMemXfer = new ImageMemXfer();
                    try
                    {
                        imageMemXfer.Memory.Flags  = MemoryFlags.AppOwns | MemoryFlags.Pointer;
                        imageMemXfer.Memory.Length = setupMemXfer.MinBufSize;                                                                 // 對於A8 scanner,Preferred = MaxBufSize,太大了,所以我們選小一點的
                        imageMemXfer.Memory.TheMem = Kernel32Native.GlobalAlloc(GlobalAllocFlags.MemFixed, (int)setupMemXfer.MinBufSize * 2); // 不知道為什麼原本她size寫要*2倍
                        imageMemXfer.Compression   = Compression.None;

                        if (imageMemXfer.Memory.TheMem == IntPtr.Zero)
                        {
                            Logger.WriteLog(LOG_LEVEL.LL_SERIOUS_ERROR, "error allocating buffer for memory transfer");
                            throw new TwainException("error allocating buffer for memory transfer");
                        }

                        long pixels_written = 0;
                        long total_pixels   = imageInfo.ImageWidth * imageInfo.ImageLength;

                        do
                        {
                            // perform a transfer
                            result = Twain32Native.DsImageMemXfer(
                                ApplicationId,
                                DataSource.SourceId,
                                DataGroup.Image,
                                DataArgumentType.ImageMemXfer,
                                Message.Get,
                                imageMemXfer
                                );

                            //string savePath = @"C:\Users\Tenny\Pictures\TwainTest\tempBitmap_";
                            //savePath += i.ToString() + @".bmp";
                            if (result == TwainResult.Success || result == TwainResult.XferDone)
                            {
                                // dibArray是這次Buffer的RGB陣列
                                byte[] dibArray = ShiftPixels(ref imageMemXfer, imageInfo.BitsPerPixel / 8);

                                BitmapRenderer.TransferPixels(bitmap, imageInfo, imageMemXfer);
                                pixels_written += (imageMemXfer.BytesWritten * 8) / imageInfo.BitsPerPixel;
                                double percent_complete = (double)pixels_written / (double)total_pixels;

                                if (result == TwainResult.XferDone)
                                {
                                    percent_complete = 1.0;
                                    // 算出空白區域的高度,裁切尾端部分
                                    int blankHeight = GetCropHeight(bitmap);
                                    if (blankHeight > 0 && blankHeight < imageInfo.ImageLength)
                                    {
                                        bitmap = cropImage(bitmap, blankHeight);
                                    }
                                }

                                // fire the transfer event
                                TransferImageEventArgs args = new TransferImageEventArgs(bitmap, result != TwainResult.XferDone, (float)percent_complete);
                                TransferImage(this, args);
                                if (!args.ContinueScanning)
                                {
                                    result = TwainResult.XferDone;
                                }
                            }
                            recievedBlockCount++;
                        } while (result == TwainResult.Success);
                    }
                    finally
                    {
                        if (imageMemXfer.Memory.TheMem != IntPtr.Zero)
                        {
                            Kernel32Native.GlobalFree(imageMemXfer.Memory.TheMem);
                            imageMemXfer.Memory.TheMem = IntPtr.Zero;
                        }
                    }

                    // End pending transfers
                    result = Twain32Native.DsPendingTransfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Control,
                        DataArgumentType.PendingXfers,
                        Message.EndXfer,
                        pendingTransfer);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }
                }while (pendingTransfer.Count != 0);
            }
            finally
            {
                // Reset any pending transfers
                result = Twain32Native.DsPendingTransfer(
                    ApplicationId,
                    DataSource.SourceId,
                    DataGroup.Control,
                    DataArgumentType.PendingXfers,
                    Message.Reset,
                    pendingTransfer);
                Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, "TransferPicturesIncremental...done.");
            }
        }
        protected void TransferPictures()
        {
            // see http://www.twain.org/wp-content/uploads/2017/03/TWAIN-2.4-Specification.pdf
            // page (3-20)

            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            PendingXfers pendingTransfer = new PendingXfers();
            TwainResult  result;

            try
            {
                do
                {
                    pendingTransfer.Count = 0;     // the Twain source will fill this in during DsPendingTransfer
                    IntPtr hbitmap = IntPtr.Zero;

                    // Get the image info
                    ImageInfo imageInfo = new ImageInfo();
                    result = Twain32Native.DsImageInfo(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Image,
                        DataArgumentType.ImageInfo,
                        Message.Get,
                        imageInfo);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }
                    Bitmap bitmap;
                    try {
                        // Transfer the image from the device
                        result = Twain32Native.DsImageTransfer(
                            ApplicationId,
                            DataSource.SourceId,
                            DataGroup.Image,
                            DataArgumentType.ImageNativeXfer,
                            Message.Get,
                            ref hbitmap);

                        if (result != TwainResult.XferDone)
                        {
                            DataSource.Close();
                            break;
                        }
                        if (hbitmap == IntPtr.Zero)
                        {
                            throw new TwainException("Transfer complete, but bitmap pointer is still null.");
                        }

                        bitmap = BitmapRenderer.NewBitmapFromHBitmap(hbitmap);
                    } finally {
                        if (hbitmap != IntPtr.Zero)
                        {
                            Kernel32Native.GlobalFree(hbitmap);
                            hbitmap = IntPtr.Zero;
                        }
                    }

                    // End pending transfers
                    result = Twain32Native.DsPendingTransfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Control,
                        DataArgumentType.PendingXfers,
                        Message.EndXfer,
                        pendingTransfer);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }

                    // fire the transfer event...
                    TransferImageEventArgs args = new TransferImageEventArgs(bitmap, pendingTransfer.Count != 0, 1.0f);
                    TransferImage(this, args);
                    if (!args.ContinueScanning)
                    {
                        break;
                    }
                }while (pendingTransfer.Count != 0);
            }
            finally
            {
                // Reset any pending transfers
                result = Twain32Native.DsPendingTransfer(
                    ApplicationId,
                    DataSource.SourceId,
                    DataGroup.Control,
                    DataArgumentType.PendingXfers,
                    Message.Reset,
                    pendingTransfer);
            }
        }