我如何将位图转换为byte []? [英] How do I convert a Bitmap to byte[]?

查看:133
本文介绍了我如何将位图转换为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我插入使用列表视图中插入事件的影像,试图调整从FileUpload控件的图像,然后将其保存在使用LINQ SQL数据库。



我发现一些代码来创建FileUpload控件内容的新的位图,但这是将其存储在一个文件服务器上,从的这个来源,但我需要保存该位图回SQL数据库,我想我需要转换回一个byte []格式。



那么,如何将位图转换为一个byte []格式的?



如果我要对这个错误的方式,我将不胜感激它,你可以纠正。我



下面是我的代码:

  //查找FileUpload控件
字符串文件名= uplImage.FileName;

//创建FileUpload控件
位图originalBMP =新位图(uplImage.FileContent)的内容存储的位图;

//计算新的图像尺寸
INT origWidth = originalBMP.Width;
INT origHeight = originalBMP.Height;
INT sngRatio = origWidth / origHeight;
INT newWidth = 100;
INT newHeight = sngRatio * newWidth;

//创建一个新位图,其中将保持前位大小
位图newBMP =新位图(originalBMP,newWidth,newHeight);

//创建一个基于新的位图
图形oGraphics = Graphics.FromImage(newBMP)的图形;

//设置新的图形文件
oGraphics.Smoothi​​ngMode = Smoothi​​ngMode.AntiAlias​​的性能;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

//绘制基于所述调整大小的位图
oGraphics.DrawImage(originalBMP,0,0,newWidth,newHeight)新的图形;



PHJamesDataContext DB =新PHJamesDataContext();

System.IO.MemoryStream流=新System.IO.MemoryStream();
newBMP.Save(流,System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
字节[]数据=新的字节[stream.Length]

PHJProjectPhoto myPhoto =
新PHJProjectPhoto
{
ProjectPhoto =数据,
订购日期= DateTime.Now,
ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
专案编号= selectedProjectId
};

db.PHJProjectPhotos.InsertOnSubmit(myPhoto);
db.SubmitChanges();


解决方案

您应该能够这块改为

  System.IO.MemoryStream流=新System.IO.MemoryStream(); 
newBMP.Save(流,System.Drawing.Imaging.ImageFormat.Bmp);

PHJProjectPhoto myPhoto =
新PHJProjectPhoto
{
ProjectPhoto = stream.ToArray()//<< ---这将您的流转换为字节[]
订购日期= DateTime.Now,
ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
专案编号= selectedProjectId
};


Basically I am inserting an image using the listviews inserting event, trying to resize an image from the fileupload control, and then save it in a SQL database using LINQ.

I found some code to create a new bitmap of the content in the fileupload control, but this was to store it in a file on the server, from this source, but I need to save the bitmap back into the SQL database, which I think I need to convert back into a byte[] format.

So how do I convert the bitmap to a byte[] format?

If I am going about this the wrong way I would be grateful it you could correct me.

Here is my 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();

解决方案

You should be able to change this block to

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

这篇关于我如何将位图转换为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆