J'essaie de récupérer une image stockée dans un objet blob Oracle et de la placer dans une nouvelle instance System.Drawing.Image. Je sais que je peux écrire le flux dans un fichier temp.bmp sur le disque et le lire à partir de là mais ce n'est pas suffisant pour moi. Comment convertir l'objet blob directement en une image?Comment obtenir une image stockée en tant qu'obob oracle dans un objet Image
0
A
Répondre
1
En supposant:
- vous utilisez le client Microsoft (
System.Data.OracleClient
). - vous avez une instance
OracleConnection
appropriée (connection
). - vous avez un
OracleCommand
prêt (command
, basé surSELECT my_blob FROM my_table WHERE id=xx
).
Cela devrait se présenter comme suit:
using (OracleDataReader odr=command.ExecuteReader())
{
reader.Read();
if (!dr.IsDBNull(0))
using (Stream s=(Stream)dr.GetOracleValue(0))
using (Image image=Image.FromStream(s))
return Copy(image);
}
où Copy est
public static Image Copy(Image original)
{
Image ret=new Bitmap(original.Width, original.Height);
using (Graphics g=Graphics.FromImage(ret))
{
g.DrawImageUnscaled(original, 0, 0);
g.Save();
}
return ret;
}
Voir my blog post et/ou KB 814675 pourquoi une copie est nécessaire.
0
Je sais cela utilise sql mais il devrait être similaire pour vos besoins
Dim cn As SqlConnection = Nothing
Dim cmd As SqlCommand = Nothing
Dim da As SqlDataAdapter = Nothing
Dim ms As MemoryStream = Nothing
Dim dsImage As Data.DataSet = Nothing
Dim myBytes() As Byte = Nothing
Dim imgJPG As System.Drawing.Image = Nothing
Dim msOut As MemoryStream = Nothing
Try
cn = New SqlConnection(ConnectionStrings("conImageDB").ToString)
cmd = New SqlCommand(AppSettings("sprocGetImage").ToString, cn)
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@dmhiRowno", irowno)
da = New SqlDataAdapter(cmd)
dsImage = New Data.DataSet
da.Fill(dsImage, "image")
If dsImage.Tables(0).Rows.Count = 0 Then
Throw New Exception("No results returned for rowno")
End If
myBytes = dsImage.Tables(0).Rows(0)("Frontimage")
ms = New MemoryStream
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = System.Drawing.Image.FromStream(ms)
'Export to JPG Stream
msOut = New MemoryStream
imgJPG.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg)
imgJPG.Dispose()
imgJPG = Nothing
ms.Close()
sFrontImage = Convert.ToBase64String(msOut.ToArray())
dsImage = New Data.DataSet
da.Fill(dsImage, "image")
myBytes = dsImage.Tables(0).Rows(0)("Backimage")
ms = New MemoryStream
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = System.Drawing.Image.FromStream(ms)
sBackImage = Convert.ToBase64String(ms.ToArray)
Catch ex As System.IO.IOException ' : An I/O error occurs.
Throw ex
Catch ex As System.ArgumentNullException ': buffer is null.
Throw ex
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.
Throw ex
Catch ex As System.ArgumentOutOfRangeException ': offset or count are negative.
Throw ex
Catch ex As System.ObjectDisposedException ' : The current stream instance is closed.
Throw ex
Catch ex As System.ArgumentException
Throw ex
Catch ex As System.Runtime.InteropServices.ExternalException ': The image was saved with the wrong image format
Throw ex
Catch ex As Exception
Throw ex
Finally
If cn IsNot Nothing Then
cn.Close()
cn.Dispose()
cn = Nothing
End If
If cmd IsNot Nothing Then
cmd.Dispose()
cmd = Nothing
End If
If da IsNot Nothing Then
da.Dispose()
da = Nothing
End If
If ms IsNot Nothing Then
ms.Dispose()
ms = Nothing
End If
If msOut IsNot Nothing Then
msOut.Close()
msOut.Dispose()
msOut = Nothing
End If
If dsImage IsNot Nothing Then
dsImage.Dispose()
dsImage = Nothing
End If
If myBytes IsNot Nothing Then
myBytes = Nothing
End If
If imgJPG IsNot Nothing Then
imgJPG.Dispose()
imgJPG = Nothing
End If
End Try
Ce code retire un jpeg enveloppé tiff pour l'image avant de sorte que le code est un peu différent de celui de l'image en arrière.
Questions connexes
- 1. Oracle + C# Image
- 2. Comment obtenir une image date créée en Java
- 3. comment repeindre une applet en déplaçant une image-objet?
- 4. Trouver une image dans une image C#
- 5. Comment convertir une image Pyglet en image PIL?
- 6. Trouver une Image dans une Image
- 7. Accéder à une image dans une image
- 8. Comment retourner une image/texture dans OpenGLES?
- 9. Comment transformer un NSMutableData en une image dans une vue?
- 10. Découpe une image en mosaïques
- 11. Comment ajouter une image en tant que préchargeur dans un fichier Flash CS4?
- 12. Comment copier une image et un texte dans le presse-papiers en tant qu'objet?
- 13. Comment rendre l'animation WPF en tant que vidéo image par image?
- 14. comment exporter une image dans un dossier?
- 15. Conversion d'une image 32bpp en une image 16bpp en Java
- 16. Comment centrer l'alignement d'une image-objet?
- 17. Image personnalisée tout en faisant glisser un objet dérivé CWnd
- 18. Comment obtenir une image couleur dans l'iPhone sdk
- 19. Comment mieux sérialiser une image java.awt.Image?
- 20. Comment puis-je ajouter une image sur une image en PHP comme un filigrane
- 21. Obtenir la première image vidéo en javascript
- 22. Comment estomper une image?
- 23. Cliquez sur une image, obtenir des coordonnées
- 24. Obtenir une représentation en mémoire du rendu JPEG d'une image
- 25. sharepoint -utilise un champ de recherche pour obtenir une image
- 26. Comment redimensionner une image?
- 27. Comment obtenir une image avec son nom et une extension?
- 28. Comment obtenir une icône à partir d'une image png?
- 29. Comment envoyer un objet image via le socket ...?
- 30. Xorg charger une image