Craete tiff图片页面 [英] Craete tiff Image Page by Page

查看:57
本文介绍了Craete tiff图片页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请帮我用C#编写一个dll,它将从文件和内存中读取每页

tiff图像流对象(需要两种方式)和

creatre一个新的tiff图像对象.dll应该返回合并的tif图像

对象。


提前提前


Rinu GP

解决方案

上次我需要弄多个页面TIF,我使用ImageMan

(当时是ActiveX)。也许现成的这样的货架会有帮助吗?这里有
?例如ImageMan .Net。 (在你问之前 - 我只是一个满意的

用户)。


或谷歌免费选项...


Marc


此示例方法接受以MemoryStreams形式传入的单个已加载的Tiff页面数组

,并在单个Tiff中返回包含

组合图像的单个MemoryStream。要将Tiff加载到memoryStream中,只需使用Filestream将其读入字节数组,然后创建一个新的MemoryStream

将字节数组传递给重载的MemoryStream构造函数。


public static System.IO.MemoryStream Join(System.IO.MemoryStream []

tifsStream)

{

EncoderParameters ep = null;

System.IO.MemoryStream singleStream = new

System.IO.MemoryStream();


尝试

{

Image imgTif = Image.FromStream(tifsStream [0]);

long imgCompression = GetCompression(imgTif );


if(tifsStream.Length 1)

{

//

//多帧

//

ep = new EncoderParameters(2);

ep.Param [0] = new EncoderParameter(Encoder.SaveFlag ,

(长)EncoderValue.MultiFrame);

ep.Param [1] =新

EncoderParameter(Encoder.Compression,imgCompression);

}

其他

{

//

//单页

//

ep = new EncoderParameters( 1);

ep.Param [0] = new

EncoderParameter(Encoder.Compression,imgCompression);

}
< br $>
//

//保存第一页

//

imgTif.Save(singleStream,CodecInfo,ep );


if(tifsStream.Length 1)

{

ep = new EncoderParameters(2);

ep.Param [0] = new EncoderParameter(Encoder.SaveFlag,

(long)EncoderValue.FrameDimensionPage);


//

//添加其余页面

//

for(int i = 1;我< tifsStream.Length; i ++)

{

Image pgTif = Image.FromStream(tifsStream [i]);


imgCompression = GetCompression(pgTif) ;

ep.Param [1] =新

EncoderParameter(Encoder.Compression,imgCompression);


imgTif.SaveAdd( pgTif,ep);

}


//

//承诺所有更改

/ /

ep = new EncoderParameters(1);


ep.Param [0] = new EncoderParameter(Encoder.SaveFlag,

(长)EncoderValue.Flush);

imgTif.SaveAdd(ep);

}

}

catch(例外)

{


throw;

}

终于

{

if(ep!= null)

ep.Dispose();

}


返回singleStream;

}


///< summary>

///创建一个新字节数组(TIF格式)通过组合一个数组

的字节数组(TIF格式)。

///< / summary>

/ //< param name =" atif">一个字节数组数组< / param>

///< returns>一个字节数组。< / returns>

public static byte [] Join(byte [] [] atif)

{

try

{

系统。 IO.MemoryStream [] multiStream = new

System.IO.MemoryStream [atif.GetLength(0)];


for(int i = 0;我< multiStream.Length; i ++)

multiStream [i] = new System.IO.MemoryStream(atif [i]);


System.IO.MemoryStream ms = Join(multiStream) );

返回ms.ToArray();

}

catch(例外)

{


throw;

}


}

///< summary>

///将输入MemoryStream(TIF格式)拆分为

MemoryStream(TIF格式)数组。

///< / summary> ;

///< param name =" tifStream"> A MemoryStream(TIF格式)。< / param>

///< returns>一个MemoryStream数组(TIF格式)。< / returns>

public static System.IO.MemoryStream []

Split(System.IO.MemoryStream tifStream)

{

System.IO.MemoryStream [] multiStream = {};

EncoderParameters ep = null;

Image tifImage = null;


尝试

{

tifImage = Image.FromStream(tifStream);


int pgCount = tifI mage.GetFrameCount(FrameDimension.Page);

multiStream = new System.IO.MemoryStream [pgCount];


for(int i = 0;我< pgCount; i ++)

{

tifImage.SelectActiveFrame(FrameDimension.Page,i);


multiStream [i] =新系统。 IO.MemoryStream();

long imgCompression = GetCompression(tifImage);


ep = new EncoderParameters(1);

ep.Param [0] = new

EncoderParameter(Encoder.Compression,imgCompression);


tifImage.Save(multiStream [i],CodecInfo,ep) ;

}

}

catch(例外)

{

throw;

}

终于

{

if(ep!= null)

ep.Dispose();


if(tifImage!= null)

tifImage.Dispose();

}

返回multiStream;

}


-

网站: http://www.eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot.com
短网址&更多: http://ittyurl.net


Rinu Gopalakrishna Pillai写道:





请帮我写一个用C#编写的dll,它会读取一个

tiff图像来自文件和内存流对象(需要两种方式)和

creatre一个新的tiff图像对象.dll应该返回合并的tif图像

对象。


提前提前


Rinu GP

嗨彼得,


非常感谢您的快速回复。


您能否告诉我如何保存创建的内容单个版本作为单个

tiff图像。将压缩作为LZW给出。代码正在运行,但在>
的情况下,CompressionCCITT4的情况也是如此。它显示异常。


彼得可以告诉我,有没有更好更快的方法返回

来自dll以外的dll的tiff图像缓冲区


谢谢和问候

Rinu GP


" Peter Bromberg [C#MVP]"写道:


此示例方法接受您作为MemoryStreams传入的单个加载的Tiff页面数组

,并返回单个MemoryStream包含单个Tiff中的

组合图像。要将Tiff加载到memoryStream中,只需使用Filestream将其读入字节数组,然后创建一个新的MemoryStream

将字节数组传递给重载的MemoryStream构造函数。


public static System.IO.MemoryStream Join(System.IO.MemoryStream []

tifsStream)

{

EncoderParameters ep = null;

System.IO.MemoryStream singleStream = new

System.IO.MemoryStream();


尝试

{

Image imgTif = Image.FromStream(tifsStream [0]);

long imgCompression = GetCompression(imgTif );


if(tifsStream.Length 1)

{

//

//多帧

//

ep = new EncoderP arameters(2);

ep.Param [0] = new EncoderParameter(Encoder.SaveFlag,

(long)EncoderValue.MultiFrame);

ep.Param [1] = new

EncoderParameter(Encoder.Compression,imgCompression);

}

else

{

//

//单页

//

ep = new EncoderParameters(1) ;

ep.Param [0] =新

EncoderParameter(Encoder.Compression,imgCompression);

}

//

//保存第一页

//

imgTif.Save(singleStream,CodecInfo,ep);


if(tifsStream.Length 1)

{

ep = new EncoderParameters(2);

ep.Param [0] = new EncoderParameter(Encoder.SaveFlag,

(long) EncoderValue.FrameDimensionPage);


//

//添加其余页面

//

for(int i = 1;我< tifsStream.Length; i ++)

{

Image pgTif = Image.FromStream(tifsStream [i]);


imgCompression = GetCompression(pgTif) ;

ep.Param [1] =新

EncoderParameter(Encoder.Compression,imgCompression);


imgTif.SaveAdd( pgTif,ep);

}


//

//承诺所有更改

/ /

ep = new EncoderParameters(1);


ep.Param [0] = new EncoderParameter(Encoder.SaveFlag,

(长)EncoderValue.Flush);

imgTif.SaveAdd(ep);

}

}

catch(例外)

{


抛出;

}

终于

{

if(ep!= null)

ep.Dispose();

}


返回singleStream;

}


///< summary>

///通过组合数组

的字节数组(TIF格式)来创建一个新的字节数组(TIF格式)。

///< / summary>

///< param name =" atif">一个字节数组数组< / param>

///< returns>一个字节数组。< / returns>

public static byte [] Join(byte [] [] atif)

{

试试

{

System.IO.MemoryStream [] multiStream = new

System.IO。 MemoryStream [atif.GetLength(0)];


for(int i = 0;我< multiStream.Length; i ++)

multiStream [i] = new System.IO.MemoryStream(atif [i]);


System.IO.MemoryStream ms = Join(multiStream) );

返回ms.ToArray();

}

catch(例外)

{


throw;

}


}

///< summary>

///将输入MemoryStream(TIF格式)拆分为

MemoryStream(TIF格式)数组。

///< / summary> ;

///< param name =" tifStream"> A MemoryStream(TIF格式)。< / param>

///< returns>一个MemoryStream数组(TIF格式)。< / returns>

public static System.IO.MemoryStream []

Split(System.IO.MemoryStream tifStream)

{

System.IO.MemoryStream [] multiStream = {};

EncoderParameters ep = null;

图片tifImage = null;


try

{

tifImage = Image.FromStream(tifStream);


int pgCount = tifImage.GetFrameCount(FrameDimension。 Page);

multiStream = new System.IO.MemoryStream [pgCount];


for(int i = 0;我< pgCount; i ++)

{

tifImage.SelectActiveFrame(FrameDimension.Page,i);


multiStream [i] =新系统。 IO.MemoryStream();

long imgCompression = GetCompression(tifImage);


ep = new EncoderParameters(1);

ep.Param [0] = new

EncoderParameter(Encoder.Compression,imgCompression);


tifImage.Save(multiStream [i],CodecInfo,ep) ;

}

}

catch(例外)

{

throw;

}

终于

{

if(ep!= null)

ep.Dispose();


if(tifImage!= null)

tifImage.Dispose();

}


返回multiStream;

}


-

网站: http://www.eggheadcafe。 com

UnBlog: http://petesbloggerama.blogspot.com

短网址&更多: http://ittyurl.net


Rinu Gopalakrishna Pillai写道:





请帮我写一个用C#编写的dll,它会读取一个

tiff图像来自文件和内存流对象(需要两种方式)和

creatre一个新的tiff图像对象.dll应该返回合并的tif图像

对象。


提前提货


Rinu GP


Hi,

Please help me to write a dll in C# , that will read each pages of a
tiff image from a file and a memory stream object ( need two ways) and
creatre a new tiff image object.The dll should return the combined tif image
object.

Thnks in advance

Rinu G P

解决方案

Last time I needed to mess with multi-page TIFs, I used ImageMan
(ActiveX at the time). Perhaps an off-the-shelf like this would help
here? e.g. ImageMan .Net. (before you ask - I''m just a satisfied
user).

Or google for a free option...

Marc


This sample methods accepts an array of your individual loaded Tiff "pages"
passed in as MemoryStreams, and returns a single MemoryStream containing the
combined images in a single Tiff. To load a Tiff into a memoryStream, just
use a Filestream to read it into a byte array, then create a new MemoryStream
passing the byte array into the overloaded MemoryStream constructor.

public static System.IO.MemoryStream Join(System.IO.MemoryStream[]
tifsStream)
{
EncoderParameters ep = null;
System.IO.MemoryStream singleStream = new
System.IO.MemoryStream();

try
{
Image imgTif = Image.FromStream(tifsStream[0]);
long imgCompression = GetCompression(imgTif);

if (tifsStream.Length 1)
{
//
//Multi-Frame
//
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);
}
else
{
//
//Single page
//
ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);
}

//
//Save the first page
//
imgTif.Save(singleStream, CodecInfo, ep);

if (tifsStream.Length 1)
{
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);

//
//Add the rest of pages
//
for (int i = 1; i < tifsStream.Length; i++)
{
Image pgTif = Image.FromStream(tifsStream[i]);

imgCompression = GetCompression(pgTif);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);

imgTif.SaveAdd(pgTif, ep);
}

//
//Commit all changes
//
ep = new EncoderParameters(1);

ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.Flush);
imgTif.SaveAdd(ep);
}
}
catch (Exception)
{

throw;
}
finally
{
if (ep != null)
ep.Dispose();
}

return singleStream;
}

/// <summary>
/// Creates a new byte array (TIF format) by combining an array
of byte arrays(TIF format).
/// </summary>
/// <param name="atif">An array of byte arrays</param>
/// <returns>An byte array.</returns>
public static byte[] Join(byte[][] atif)
{
try
{
System.IO.MemoryStream[] multiStream = new
System.IO.MemoryStream[atif.GetLength(0)];

for (int i = 0; i < multiStream.Length; i++)
multiStream[i] = new System.IO.MemoryStream(atif[i]);

System.IO.MemoryStream ms = Join(multiStream);
return ms.ToArray();
}
catch (Exception)
{

throw;
}

}
/// <summary>
/// Splits an input MemoryStream (TIF format) into an array of
MemoryStream (TIF format).
/// </summary>
/// <param name="tifStream">A MemoryStream (TIF format).</param>
/// <returns>An array of MemoryStream(TIF format).</returns>
public static System.IO.MemoryStream[]
Split(System.IO.MemoryStream tifStream)
{
System.IO.MemoryStream[] multiStream ={};
EncoderParameters ep = null;
Image tifImage = null;

try
{
tifImage = Image.FromStream(tifStream);

int pgCount = tifImage.GetFrameCount(FrameDimension.Page);
multiStream = new System.IO.MemoryStream[pgCount];

for (int i = 0; i < pgCount; i++)
{
tifImage.SelectActiveFrame(FrameDimension.Page, i);

multiStream[i] = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);

ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);

tifImage.Save(multiStream[i], CodecInfo, ep);
}
}
catch (Exception)
{
throw;
}
finally
{
if (ep != null)
ep.Dispose();

if (tifImage != null)
tifImage.Dispose();
}
return multiStream;
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rinu Gopalakrishna Pillai" wrote:

Hi,

Please help me to write a dll in C# , that will read each pages of a
tiff image from a file and a memory stream object ( need two ways) and
creatre a new tiff image object.The dll should return the combined tif image
object.

Thnks in advance

Rinu G P


Hi Peter,

Thanks a lot for your quick response.

Can you please tell me how can I save the created single straem as a single
tiff image.While giving the compression as "LZW" the code is working but in
the case of "CompressionCCITT4" its showing exception.

Peter can u advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer"

Thanks and Regards
Rinu G P

"Peter Bromberg [C# MVP]" wrote:

This sample methods accepts an array of your individual loaded Tiff "pages"
passed in as MemoryStreams, and returns a single MemoryStream containing the
combined images in a single Tiff. To load a Tiff into a memoryStream, just
use a Filestream to read it into a byte array, then create a new MemoryStream
passing the byte array into the overloaded MemoryStream constructor.

public static System.IO.MemoryStream Join(System.IO.MemoryStream[]
tifsStream)
{
EncoderParameters ep = null;
System.IO.MemoryStream singleStream = new
System.IO.MemoryStream();

try
{
Image imgTif = Image.FromStream(tifsStream[0]);
long imgCompression = GetCompression(imgTif);

if (tifsStream.Length 1)
{
//
//Multi-Frame
//
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);
}
else
{
//
//Single page
//
ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);
}

//
//Save the first page
//
imgTif.Save(singleStream, CodecInfo, ep);

if (tifsStream.Length 1)
{
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);

//
//Add the rest of pages
//
for (int i = 1; i < tifsStream.Length; i++)
{
Image pgTif = Image.FromStream(tifsStream[i]);

imgCompression = GetCompression(pgTif);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);

imgTif.SaveAdd(pgTif, ep);
}

//
//Commit all changes
//
ep = new EncoderParameters(1);

ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.Flush);
imgTif.SaveAdd(ep);
}
}
catch (Exception)
{

throw;
}
finally
{
if (ep != null)
ep.Dispose();
}

return singleStream;
}

/// <summary>
/// Creates a new byte array (TIF format) by combining an array
of byte arrays(TIF format).
/// </summary>
/// <param name="atif">An array of byte arrays</param>
/// <returns>An byte array.</returns>
public static byte[] Join(byte[][] atif)
{
try
{
System.IO.MemoryStream[] multiStream = new
System.IO.MemoryStream[atif.GetLength(0)];

for (int i = 0; i < multiStream.Length; i++)
multiStream[i] = new System.IO.MemoryStream(atif[i]);

System.IO.MemoryStream ms = Join(multiStream);
return ms.ToArray();
}
catch (Exception)
{

throw;
}

}
/// <summary>
/// Splits an input MemoryStream (TIF format) into an array of
MemoryStream (TIF format).
/// </summary>
/// <param name="tifStream">A MemoryStream (TIF format).</param>
/// <returns>An array of MemoryStream(TIF format).</returns>
public static System.IO.MemoryStream[]
Split(System.IO.MemoryStream tifStream)
{
System.IO.MemoryStream[] multiStream ={};
EncoderParameters ep = null;
Image tifImage = null;

try
{
tifImage = Image.FromStream(tifStream);

int pgCount = tifImage.GetFrameCount(FrameDimension.Page);
multiStream = new System.IO.MemoryStream[pgCount];

for (int i = 0; i < pgCount; i++)
{
tifImage.SelectActiveFrame(FrameDimension.Page, i);

multiStream[i] = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);

ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);

tifImage.Save(multiStream[i], CodecInfo, ep);
}
}
catch (Exception)
{
throw;
}
finally
{
if (ep != null)
ep.Dispose();

if (tifImage != null)
tifImage.Dispose();
}
return multiStream;
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rinu Gopalakrishna Pillai" wrote:

Hi,

Please help me to write a dll in C# , that will read each pages of a
tiff image from a file and a memory stream object ( need two ways) and
creatre a new tiff image object.The dll should return the combined tif image
object.

Thnks in advance

Rinu G P


这篇关于Craete tiff图片页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆