首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将位图转换为byte[]?

如何将位图转换为byte[]?
EN

Stack Overflow用户
提问于 2008-11-06 09:09:50
回答 3查看 33.4K关注 0票数 15

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

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

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

如果我做错了这件事,我会很感激你能纠正我。

下面是我的代码:

代码语言:javascript
复制
            // 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();
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-11-06 09:18:04

您应该能够将此块更改为

代码语言:javascript
复制
        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
            };
票数 23
EN

Stack Overflow用户

发布于 2008-11-06 09:16:02

如果您已经有了MemoryStream,只需调用MemoryStream.ToArray获取数据即可。

票数 13
EN

Stack Overflow用户

发布于 2008-11-06 09:15:27

假设您的位图是bmp

代码语言:javascript
复制
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();
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/268013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档