U
udit_handa
I am facing a strange problem I ahve a datadase which stores tiff
images using Image datatype in Sql server.
These images are written using a Vb6.0 desktop application with the
following code
==================VB Code====================================
Open strFile For Binary Access Read As #(iFile)
ImageSize = LOF(iFile)
lngNumChunks = ImageSize / 4096
lngOff = ImageSize Mod 4096
strData = String(4096, "0")
For i = 1 To lngNumChunks
strData = String(4096, "0")
Get #(iFile), , strData
rs.Fields("Img").AppendChunk strData
Next
If lngOff > 0 Then
strData = String(lngOff, "0")
Get #(iFile), , strData
rs.Fields("Img").AppendChunk strData
End If
Close #(iFile)
====================VB Code==================================
It works fine when i read it with a VB6.0 application but i have tried
my level best without sucess to read this image file in Dot net.
A sample code that i last tried is as follows
=========Dot Net C# code===========================
SqlCommand cmd=new SqlCommand();
SqlDataReader rdr;
cmd.CommandType=CommandType.Text ;
cmd.CommandText="select img from tblDAllImages where Active = 1 and ID
=1135";
cmd.Connection=con;
con.Open ();
rdr=cmd.ExecuteReader();
System.IO.FileStream fs ;//= new System.IO.FileStream("c:\\aa.tif",
System.IO.FileMode.Create, System.IO.FileAccess.Write);
BinaryWriter bw ;
int BufferSize=100;
byte[] b =new byte[BufferSize];
long retval,startindex=0;
for(int i=0;i<b.Length ;i++)
Response.Write(b);
Response.Write(b.Length);
if (rdr.Read())
{
fs = new FileStream("c:\\aaa11.tif" , FileMode.OpenOrCreate,
FileAccess.Write) ;
bw = new BinaryWriter(fs) ;
startindex = 0 ;
retval = rdr.GetBytes(0 , startindex, b, 0, BufferSize) ;
while( retval == BufferSize )
{
bw.Write(b) ;
bw.Flush();
startindex = startindex + BufferSize;
retval = rdr.GetBytes(0, startindex, b, 0, BufferSize) ;
}
bw.Write(b) ;
bw.Flush();
bw.Close() ;
fs.Close();
}
=========Dot Net C# code===========================
This file gets created but is corrupted.It is almost double the size of
the VB6.0 file.
I guess it is something retated to conversion between byte and string
type but i have even tried to convert the byte to a string and then
save it.
Any help is welcome
Thanks
images using Image datatype in Sql server.
These images are written using a Vb6.0 desktop application with the
following code
==================VB Code====================================
Open strFile For Binary Access Read As #(iFile)
ImageSize = LOF(iFile)
lngNumChunks = ImageSize / 4096
lngOff = ImageSize Mod 4096
strData = String(4096, "0")
For i = 1 To lngNumChunks
strData = String(4096, "0")
Get #(iFile), , strData
rs.Fields("Img").AppendChunk strData
Next
If lngOff > 0 Then
strData = String(lngOff, "0")
Get #(iFile), , strData
rs.Fields("Img").AppendChunk strData
End If
Close #(iFile)
====================VB Code==================================
It works fine when i read it with a VB6.0 application but i have tried
my level best without sucess to read this image file in Dot net.
A sample code that i last tried is as follows
=========Dot Net C# code===========================
SqlCommand cmd=new SqlCommand();
SqlDataReader rdr;
cmd.CommandType=CommandType.Text ;
cmd.CommandText="select img from tblDAllImages where Active = 1 and ID
=1135";
cmd.Connection=con;
con.Open ();
rdr=cmd.ExecuteReader();
System.IO.FileStream fs ;//= new System.IO.FileStream("c:\\aa.tif",
System.IO.FileMode.Create, System.IO.FileAccess.Write);
BinaryWriter bw ;
int BufferSize=100;
byte[] b =new byte[BufferSize];
long retval,startindex=0;
for(int i=0;i<b.Length ;i++)
Response.Write(b);
Response.Write(b.Length);
if (rdr.Read())
{
fs = new FileStream("c:\\aaa11.tif" , FileMode.OpenOrCreate,
FileAccess.Write) ;
bw = new BinaryWriter(fs) ;
startindex = 0 ;
retval = rdr.GetBytes(0 , startindex, b, 0, BufferSize) ;
while( retval == BufferSize )
{
bw.Write(b) ;
bw.Flush();
startindex = startindex + BufferSize;
retval = rdr.GetBytes(0, startindex, b, 0, BufferSize) ;
}
bw.Write(b) ;
bw.Flush();
bw.Close() ;
fs.Close();
}
=========Dot Net C# code===========================
This file gets created but is corrupted.It is almost double the size of
the VB6.0 file.
I guess it is something retated to conversion between byte and string
type but i have even tried to convert the byte to a string and then
save it.
Any help is welcome
Thanks