如何在C#中将TIFF图像添加到另一个图像的底部?

时间:2012-06-04 23:44:09

标签: c# tiff twain

我使用TWAIN扫描双面图像,它为我提供了一系列图片,我可以单独保存。 我需要将这两个图像并排组合并将其保存为单个TIFF文件。

您能告诉我如何打开TIFF图像并将它们保存为一个并排包含这两个图像的文件吗?

2 个答案:

答案 0 :(得分:1)

您是否根据TWAIN规范编写自己的应用?如果是,您可以使用TWFF_TIFFMULTI获取多页TIFF文件。

答案 1 :(得分:0)

var tmpFolder = _configManager.TmpFolder;
bool isUnHandleException;
//tranfer each image that's scann0ed and insert him to array,also dialogbox for a progress bar Indication  
ArrayList pics = tw.TransferPictures(out isUnHandleException);
EndingScan(); tw.CloseSrc();
if (isUnHandleException)   ShowException(Consts.RestartWIA);

string strFileName = "";
// join all the images that's scanned  to one image tiff file
var encoder = new TiffBitmapEncoder();
BitmapFrame frame=null;
Bitmap bp = null;
IntPtr bmpptr,pixptr;
if (!(pics != null && pics.Count != 0))
{
    ShowException("No Has Any pages");
    return;
}
// create temp folder 
CreateDirectory(tmpFolder);
int picsCount = pics.Count;
for (int i = 0; i < pics.Count; i++)
{
    IntPtr img = (IntPtr)pics[i];
    //Locks a global memory object and returns a pointer to the first byte of the object's memory block 
    bmpptr = Twain.GlobalLock(img);
    //Get Pixel Info by handle
    pixptr = GdiWin32.GetPixelInfo(bmpptr);
    // create bitmap type
    bp = GdiWin32.BitmapFromDIB(bmpptr, pixptr);

    // get bitmap frame for insert him TiffBitmapEncoder
    frame = Imaging.GetBitmapFrame(bp);
    if (frame != null)
        encoder.Frames.Add(frame);
    //decease pointer reference so the gc will see there is no refernce to this obj and realse him from memory
    Twain.GlobalUnlock(img);
    // Get the last error and display it.
    //int error = Marshal.GetLastWin32Error();

    GC.Collect();
    GC.WaitForPendingFinalizers();
}
// genrate file name to temp folder 
strFileName = GenerateFileTemp(tmpFolder);
string strNewFileName = strFileName;
_output = new FileStream(strNewFileName, FileMode.OpenOrCreate);
encoder.Save(_output);