c# - 如何将 Bitmap 转换为 byte[]?

标签 c# linq asp.net-3.5 byte image-uploading

基本上,我使用 ListView 插入事件插入图像,尝试从文件上传控件调整图像大小,然后使用 LINQ 将其保存在 SQL 数据库中。

我找到了一些代码来创建 fileupload 控件中内容的新位图,但这是为了将其存储在服务器上的文件中,来自 this source ,但我需要将位图保存回 SQL 数据库,我认为我需要将其转换回 byte[] 格式。

那么如何将位图转换为 byte[] 格式呢?

如果我以错误的方式解决这个问题,我将不胜感激,您可以纠正我。

这是我的代码:

            // 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();

最佳答案

您应该能够将此 block 更改为

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

关于c# - 如何将 Bitmap 转换为 byte[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/268013/

相关文章:

c# - 列表框内的复选框

c# - 防止 Xamarin Android 应用程序中的方向更改

c# - 从 2 个查询中选择更好的查询,都返回相同的结果

c# - 如何全局取消特定业务逻辑的回发事件?

c# - 所有值的计数均为零的频率表

C# LINQ 过滤列表

wcf - 为什么我的 WCF 服务不响应 web.config 中的 baseAddress 设置?

c# - RDLC 中的特定格式编号

javascript - 根据 AJAX 请求的类型显示 LoadingGif

c# - 在新选项卡中打开图片,就像右键单击一样 - 在 firefox 中查看图片 (WatiN)