문제

기본적으로 ListViews 삽입 이벤트 삽입을 사용하여 이미지를 삽입하고 FileUpload Control에서 이미지를 조정 한 다음 LINQ를 사용하여 SQL 데이터베이스에 저장하려고합니다.

FileUpload 컨트롤에서 컨텐츠의 새 비트 맵을 만들기위한 코드를 찾았지만이를 서버의 파일에 저장하는 것이 었습니다. 이 소스, 그러나 비트 맵을 SQL 데이터베이스에 다시 저장해야합니다. 이는 바이트 [] 형식으로 다시 변환해야한다고 생각합니다.

그렇다면 비트 맵을 바이트 [] 형식으로 어떻게 변환합니까?

내가 이것에 대해 잘못하고 있다면 나는 그것을 고맙게 생각할 것입니다. 당신은 나를 교정 할 수 있습니다.

내 코드는 다음과 같습니다.

            // 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();
도움이 되었습니까?

해결책

이 블록을 변경할 수 있어야합니다

        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
            };

다른 팁

당신이있는 경우 MemoryStream 이미 전화하십시오 MemoryStream.ToArray 데이터를 꺼내려면.

비트 맵이 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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top