Question

En principe, j'insère une image à l'aide de l'événement d'insertion de listviews, j'essaie de redimensionner une image à partir du contrôle fileupload, puis je l'enregistre dans une base de données SQL à l'aide de LINQ.

J'ai trouvé du code pour créer une nouvelle image bitmap du contenu dans le contrôle fileupload, mais il s'agissait de le stocker dans un fichier sur le serveur, à partir de cette source , mais je dois réenregistrer le bitmap dans la base de données SQL, que je pense devoir reconvertir au format octet [].

Alors, comment convertir le bitmap au format octet []?

Si je m'y prends mal, je vous serais reconnaissant de 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();
Était-ce utile?

La solution

Vous devriez pouvoir changer ce bloc en

        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

        PHJProjectPhoto myPhoto =
            new PHJProjectPhoto
            {
                ProjectPhoto = stream.ToArray(), // <<--- This will convert your stream to a byte[]
                OrderDate = DateTime.Now,
                ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
                ProjectId = selectedProjectId
            };

Autres conseils

Si vous avez déjà un MemoryStream , appelez simplement MemoryStream.ToArray pour extraire les données.

En supposant que votre bitmap soit au format bmp

byte[] data;
using(System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
   bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
   stream.Position = 0;
   data = new byte[stream.Length];
   stream.Read(data, 0, stream.Length);
   stream.Close();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top