Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[Sql Server 2005]Convertir une image stockée dans un tableau de byte []

3 réponses
Avatar
Oriane
Bonjour,

le code suivant fonctionne pour une image bitmap (*.bmp) stockée dans une
base sql Server 2005 dans un champ de type Image.

MemoryStream strm = new MemoryStream();
BitmapImage bitmap = new BitmapImage();

// Well-known work-around to make Northwind images work
int offset = 78;
strm.Write( bdIconeArray, offset, bdIconeArray.Length - offset );

// Read the image into the bitmap object
bitmap.BeginInit();
bitmap.StreamSource = strm;
bitmap.EndInit();

//BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
myimage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions() );
imgSource = bitmap;

Mais si l'image en base est de type png, jpg ou tiff ca na fonctionne
plus...

Savez-vous pourquoi ? Doit-on changer le paramètre Offset ?

Merci d'avance

3 réponses

Avatar
Gilles TOURREAU
Le Thu, 06 Dec 2007 14:54:01 +0100, Oriane a écrit:

Bonjour,

le code suivant fonctionne pour une image bitmap (*.bmp) stockée dans
une base sql Server 2005 dans un champ de type Image.

MemoryStream strm = new MemoryStream();
BitmapImage bitmap = new BitmapImage();

// Well-known work-around to make Northwind images work
int offset = 78;
strm.Write( bdIconeArray, offset, bdIconeArray.Length - offset );

// Read the image into the bitmap object
bitmap.BeginInit();
bitmap.StreamSource = strm;
bitmap.EndInit();

//BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
myimage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions() );
imgSource = bitmap;

Mais si l'image en base est de type png, jpg ou tiff ca na fonctionne
plus...

Savez-vous pourquoi ? Doit-on changer le paramètre Offset ?

Merci d'avance



Pourquoi ne pas faire plus-tôt simple :

Image image;

image =
(Image)TypeDescriptor.GetConverter(typeof(Image)).ConvertFrom(bdIconeArray);

Cordialement

--
Gilles TOURREAU


S.A.R.L. P.O.S
Le spécialiste en motoculture depuis + de 30 ans !
http://www.pos.fr
Avatar
Oriane
Bonjour,
"Gilles TOURREAU" a écrit dans le message de
news:
Le Thu, 06 Dec 2007 14:54:01 +0100, Oriane a écrit:

Pourquoi ne pas faire plus-tôt simple :

Image image;

image =
(Image)TypeDescriptor.GetConverter(typeof(Image)).ConvertFrom(bdIconeArray);


Parce que ca plante... On a déjà essayé plusieurs codes de ce genre...

Merci quand même !

Oriane
Avatar
Oriane
En creusant un peu je pense savoir d'où vient le pb. Je passe par Access
pour renseigner ma base Sql Server 2005 et je fais du OLE pour remplir mes
images. Or je suis tombé sur ceci:

"Notice a subtlety in this code that is particular of the Northwind database
but has no relevance in general. The original Access database was converted
into SQL Server's Northwind database, so the image field called Photo
doesn't contain a true GIF file; instead it contains the OLE object that
Access builds to wrap any image. As a result, the stream of bytes you read
from the field is prefixed with a header you must strip off to get the bits
of the image. Such a header is variable-length and also depends on the
length of the originally imported file's name. For Northwind, the length of
this offset is 78 bytes."

Donc ce serait à cause de ceci que la conversion planterait !

Damnède !