Comment convertir un bitmap en octet []?
Fondamentalement, je suis l'insertion d'une image à l'aide de la listviews de l'insertion de l'événement, essayez de redimensionner une image à partir du contrôle fileupload, puis de l'enregistrer dans une base de données SQL à l'aide de LINQ.
Je l'ai trouvé un peu de code pour créer une nouvelle image du contenu dans le contrôle fileupload, mais c'est pour le stocker dans un fichier sur le serveur, à partir de cette sourcemais j'ai besoin d'enregistrer l'image bitmap en arrière dans la base de données SQL, je pense que j'ai besoin de convertir en arrière dans un byte[] format.
Alors, comment puis-je convertir l'image dans un byte[] format?
Si je vais sur ce le mauvais sens, je serais reconnaissant de ce que vous pourriez me corriger.
Voici mon code:
//Find the fileUpload control
string filename = uplImage.FileName;
//Create a bitmap in memory of the content of the fileUpload control
Bitmap originalBMP = new Bitmap(uplImage.FileContent);
//Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 100;
int newHeight = sngRatio * newWidth;
//Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
//Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
//Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
PHJamesDataContext db = new PHJamesDataContext();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
PHJProjectPhoto myPhoto =
new PHJProjectPhoto
{
ProjectPhoto = data,
OrderDate = DateTime.Now,
ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
ProjectId = selectedProjectId
};
db.PHJProjectPhotos.InsertOnSubmit(myPhoto);
db.SubmitChanges();
source d'informationauteur the-undefined
Vous devez vous connecter pour publier un commentaire.
Vous devriez être en mesure de modifier ce bloc à
Si vous avez un
MemoryStream
déjà, il suffit d'appelerMemoryStream.ToArray
pour obtenir les données.En supposant, que votre image bitmap bmp