code_junkie

How to get an image stored as an oracle blob into an Image object

Nov 11th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. using (OracleDataReader odr=command.ExecuteReader())
  2. {
  3. reader.Read();
  4.  
  5. if (!dr.IsDBNull(0))
  6. using (Stream s=(Stream)dr.GetOracleValue(0))
  7. using (Image image=Image.FromStream(s))
  8. return Copy(image);
  9. }
  10.  
  11. public static Image Copy(Image original)
  12. {
  13. Image ret=new Bitmap(original.Width, original.Height);
  14. using (Graphics g=Graphics.FromImage(ret))
  15. {
  16. g.DrawImageUnscaled(original, 0, 0);
  17. g.Save();
  18. }
  19.  
  20. return ret;
  21. }
  22.  
  23. Dim cn As SqlConnection = Nothing
  24. Dim cmd As SqlCommand = Nothing
  25. Dim da As SqlDataAdapter = Nothing
  26. Dim ms As MemoryStream = Nothing
  27. Dim dsImage As Data.DataSet = Nothing
  28. Dim myBytes() As Byte = Nothing
  29. Dim imgJPG As System.Drawing.Image = Nothing
  30. Dim msOut As MemoryStream = Nothing
  31.  
  32. Try
  33. cn = New SqlConnection(ConnectionStrings("conImageDB").ToString)
  34. cmd = New SqlCommand(AppSettings("sprocGetImage").ToString, cn)
  35. cmd.CommandType = Data.CommandType.StoredProcedure
  36.  
  37. cmd.Parameters.AddWithValue("@dmhiRowno", irowno)
  38.  
  39. da = New SqlDataAdapter(cmd)
  40.  
  41. dsImage = New Data.DataSet
  42. da.Fill(dsImage, "image")
  43.  
  44. If dsImage.Tables(0).Rows.Count = 0 Then
  45. Throw New Exception("No results returned for rowno")
  46. End If
  47. myBytes = dsImage.Tables(0).Rows(0)("Frontimage")
  48.  
  49. ms = New MemoryStream
  50. ms.Write(myBytes, 0, myBytes.Length)
  51.  
  52. imgJPG = System.Drawing.Image.FromStream(ms)
  53.  
  54. 'Export to JPG Stream
  55. msOut = New MemoryStream
  56. imgJPG.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg)
  57. imgJPG.Dispose()
  58. imgJPG = Nothing
  59. ms.Close()
  60. sFrontImage = Convert.ToBase64String(msOut.ToArray())
  61.  
  62. dsImage = New Data.DataSet
  63. da.Fill(dsImage, "image")
  64. myBytes = dsImage.Tables(0).Rows(0)("Backimage")
  65.  
  66. ms = New MemoryStream
  67. ms.Write(myBytes, 0, myBytes.Length)
  68.  
  69. imgJPG = System.Drawing.Image.FromStream(ms)
  70. sBackImage = Convert.ToBase64String(ms.ToArray)
  71.  
  72. Catch ex As System.IO.IOException ' : An I/O error occurs.
  73. Throw ex
  74. Catch ex As System.ArgumentNullException ': buffer is null.
  75. Throw ex
  76. Catch ex As System.NotSupportedException ': The stream does not support writing. For additional information see System.IO.Stream.CanWrite.-or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.
  77. Throw ex
  78. Catch ex As System.ArgumentOutOfRangeException ': offset or count are negative.
  79. Throw ex
  80. Catch ex As System.ObjectDisposedException ' : The current stream instance is closed.
  81. Throw ex
  82. Catch ex As System.ArgumentException
  83. Throw ex
  84. Catch ex As System.Runtime.InteropServices.ExternalException ': The image was saved with the wrong image format
  85. Throw ex
  86. Catch ex As Exception
  87. Throw ex
  88. Finally
  89. If cn IsNot Nothing Then
  90. cn.Close()
  91. cn.Dispose()
  92. cn = Nothing
  93. End If
  94.  
  95. If cmd IsNot Nothing Then
  96. cmd.Dispose()
  97. cmd = Nothing
  98. End If
  99.  
  100. If da IsNot Nothing Then
  101. da.Dispose()
  102. da = Nothing
  103. End If
  104.  
  105. If ms IsNot Nothing Then
  106. ms.Dispose()
  107. ms = Nothing
  108. End If
  109.  
  110. If msOut IsNot Nothing Then
  111. msOut.Close()
  112. msOut.Dispose()
  113. msOut = Nothing
  114. End If
  115.  
  116. If dsImage IsNot Nothing Then
  117. dsImage.Dispose()
  118. dsImage = Nothing
  119. End If
  120.  
  121. If myBytes IsNot Nothing Then
  122. myBytes = Nothing
  123. End If
  124.  
  125. If imgJPG IsNot Nothing Then
  126. imgJPG.Dispose()
  127. imgJPG = Nothing
  128. End If
  129.  
  130. End Try
Add Comment
Please, Sign In to add comment