|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Unable to read image file correctly that was written using vb6.0images 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[i]); 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 Udit,
Some pictures have headers. what can mean that the startingpoint is 78 while you have to decrement the buffersize with that. I hope this helps, Cor <udit_ha***@hotmail.com> schreef in bericht Show quote news:1131401015.521366.54780@g44g2000cwa.googlegroups.com... >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[i]); > 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 > Hi ,
Thanks for the reply but i am able to create images using VB code without considering any headers. ======SaveImage=========== 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) ======SaveImage=========== ===Load Image============= Open strFile For Binary Access Write As #(iFile) Set fld = rs.Fields("Img") ImageSize = fld.ActualSize lngNumChunks = ImageSize \ 4096 lngOff = ImageSize Mod 4096 If lngOff > 0 Then strData = String(lngOff, "0") strData = fld.GetChunk(lngOff) Put #(iFile), , strData End If Do While lngOff < ImageSize strData = String(4096, "0") strData = fld.GetChunk(4096) lngOff = lngOff + 4096 Put #(iFile), , strData Loop Close #(iFile) ===Load Image============= This code always work with VB6.0 without considering the headers. Could the proble lie it the way the data is first read and written using a string? Thanks Udit Udit,
See here a sample how to get an image from the northwind database. http://www.vb-tips.com/default.aspx?ID=f6d5cc8c-a917-482b-ab1d-253a1ec85fc3 This is one methode to see more about images type just image in the search box on this page. I hope this helps, Cor I tried every example but it wont read the image properly.
My code works fine with the Category table Picture column in Northwind Database .But not with the one I use. For now I made a small VB6.0 component to create a file from the image field and it works fine but still i would like to use as Dot net code. My VB6.0 component code is as follows Public Sub CreateImage(ByVal ImageID As Long, ByVal FileName As String) Dim con As New ADODB.Connection con.Open Me.ConnectionString Dim rs As New ADODB.Recordset, iFile As Integer, i As Integer, strFile1 As String Dim fld As ADODB.Field, ImageSize As Long, strData As String, bytChunk() As Byte Dim lngNumChunks As Long, lngOff As Long, strFile As String rs.Open "select * from tblDAllImages where Active = 1 and ID = " & ImageID, con, adOpenKeyset, adLockOptimistic If rs.EOF = False Then iFile = FreeFile Open FileName For Binary Access Write As #(iFile) Set fld = rs.Fields("Img") ImageSize = fld.ActualSize lngNumChunks = ImageSize \ 4096 lngOff = ImageSize Mod 4096 If lngOff > 0 Then strData = String(lngOff, "0") strData = fld.GetChunk(lngOff) Put #(iFile), , strData End If Do While lngOff < ImageSize strData = String(4096, "0") strData = fld.GetChunk(4096) lngOff = lngOff + 4096 Put #(iFile), , strData Loop Close #(iFile) End If If rs.State = adStateOpen Then rs.Close Set rs = Nothing End Sub Thanks Udit Udit,
You did try the sample without the 78 offset as well I don't know almost no classic Ado code more. So with that it seems for me not helpful to help you. Cor Hi,
I did tried the 78 bit ofset and it works fine for the northwind database images but not for mine. I hope there is a solution somewhere. thanks Udit Udit,
I would not know, it should have to do how your images are written. I see no reply from Paul who mostly replies on these messages as it is ado -> adonet, so it will be hard to go. What you can try is using the newsgroup microsoft.public.dotnet.languages.vb I give you few change, however you never know Cor Show quote "Udit" <udit_ha***@hotmail.com> schreef in bericht news:eS%238XBg5FHA.1420@TK2MSFTNGP09.phx.gbl... > Hi, > I did tried the 78 bit ofset and it works fine for the northwind > database images but not for mine. > I hope there is a solution somewhere. > thanks Udit > > > -- > Sent via .NET Newsgroups > http://www.dotnetnewsgroups.com First, I would simplify how you are getting the data from the database
I do the exact same thing you are doing in some of my apps, which work with any data type. It should work for you. System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); System.Data.SqlClient.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 ; if (rdr.Read()) { //get the data from the reader byte[] pData = (byte[])rdr["img"]; //open file for writing fs = new FileStream("c:\\aaa11.tif" , FileMode.OpenOrCreate, FileAccess.Write) ; //write the bytes. (if your file size could be creater than an int can hold, you'll have to loop. fs.Write(pData,0,pData.Length); fs.Close(); } rdr.Close(); <udit_ha***@hotmail.com> wrote in message Show quote news:1131401015.521366.54780@g44g2000cwa.googlegroups.com... >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[i]); > 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 > |
|||||||||||||||||||||||