C#读写TIF文件
各位谁有tif文件读写的经验 最近有一个传真处理的项目比较头疼
浮云游子意,落日故人情MSN:[email protected]
不知道你的tif是什么版本的,如果版本比较老,直接用c#就可以读,如果是新版本的,可能得找.net 的控件了。tif的版本非常多,以前做过一个扫描仪的程序,这个tif文件连acdsee和PS都读不了。只能去下控件了。
系统生成一个图片例如“1.jpg”;
先读取文件,判断页面的数量,如果是多页面的,就读取页面数。单个的话就直接读取到1.jpg中返回。
多个页面的话,选择哪个页面就读取选择的页面,写到1.jpg中并返回。
我把一些基本的代码复制过来供大家看看。希望对大家有所帮助:
我这里用到了框架:
index.aspx页面代码:
<iframe src=<%=pppp%> height="600" width="1024"></iframe>
<br />
<asp:Label ID="Label1" runat="server" Width="140px"></asp:Label>
<aspropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="16px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
Width="166px">
</aspropDownList>
index.aspx.cs代码:
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public string address;
public string pppp;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
address = "img/123.tif";
Get_tif_Count();
pppp = "Default.aspx?kk=0";
}
}
private void Get_tif_Count()
{
Bitmap bt = new Bitmap(Server.MapPath(address));
Guid gud = (Guid)bt.FrameDimensionsList.GetValue(0);
FrameDimension fds = new FrameDimension(gud);
int totalPage = bt.GetFrameCount(fds);
if (totalPage == 1)
{
Response.Redirect("Default.aspx";
}
this.Label1.Text = "共" + totalPage + "页";
Hashtable ht = new Hashtable();
for (int i = 0; i < totalPage; i++)
{
ht.Add(i, i + 1);
}
this.DropDownList1.DataSource = ht;
this.DropDownList1.DataTextField = "Value";
this.DropDownList1.DataValueField = "Key";
this.DropDownList1.DataBind();
this.DropDownList1.SelectedValue = "0";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string key = this.DropDownList1.SelectedValue.ToString();
pppp = "Default.aspx?kk=" + key;
//Response.Redirect("Default.aspx?kk=" + key);
}
Default.aspx.cs代码:
public string address;
//public static ImageFormat Jpeg { get; }
public string pp;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
try{
pp = Request["kk"].ToString();
}
catch
{
pp = "";
}
address = "img/123.tif";
getImage(address);
}
}
void getImage(string imgpath)//根据路径输出Jpeg格式图片流
{
Bitmap bt = new Bitmap(Server.MapPath(imgpath));
Guid gud=(Guid)bt.FrameDimensionsList.GetValue(0);
FrameDimension fds=new FrameDimension(gud);
//int totalPage=bt.GetFrameCount(fds);
//this.Label1.Text = "共" + totalPage + "页";
/* for (int i = 0; i < totalPage; i++)
{
bt.SelectActiveFrame(fds, i);
}*/
if (pp != ""
{
int qq = Convert.ToInt16(pp);
bt.SelectActiveFrame(fds, qq);
}
MemoryStream ss = new MemoryStream();
bt.Save(Server.MapPath("1.jpg", ImageFormat.Jpeg);
bt.Save(ss, ImageFormat.Jpeg);
byte[] bb = ss.GetBuffer();
ss.Read(bb, 0, (int)ss.Length);
//this.ID_img.ImageUrl =Convert.ToString(bb);
Response.BinaryWrite(bb);
ss.Close();
bt.Dispose();
Response.End();
}
C#读取分割多页Tif文件
//分割Tif图片为多个Gif图片
1 Image img = Image.FromFile("C://1.tif");
2 Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
3 FrameDimension dimension = new FrameDimension(guid);
4 int totalPage = img.GetFrameCount(dimension);
5
6 this.statusBar1.Text = "共"+totalPage+"页";
7
8 for(int i=0;i<totalPage;i++)
9 {
10 img.SelectActiveFrame(dimension,i);
11 img.Save("C://Gif"+i+".gif",System.Drawing.Imaging.ImageFormat.Gif);
12 }
13