Wie konvertiere ich eine Bitmap in Byte []?

rundsätzlich füge ich ein Bild mit dem Ereignis listviews inserting ein, versuche, die Größe eines Bilds über das FileUpload-Steuerelement zu ändern, und speichere es dann mit LINQ in einer SQL-Datenban

Ich habe Code gefunden, um eine neue Bitmap des Inhalts im FileUpload-Steuerelement zu erstellen, aber dies war, um ihn in einer Datei auf dem Server von @ zu speicherdiese Quelle, aber ich muss die Bitmap wieder in der SQL-Datenbank speichern, die ich denke, ich muss wieder in ein Byte [] -Format konvertieren.

Wie konvertiere ich die Bitmap in ein Byte [] -Format?

Wenn ich das falsch mache, wäre ich dankbar, dass du mich korrigieren könntest.

Hier ist mein 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();

Antworten auf die Frage(3)

Ihre Antwort auf die Frage