C# 如何将位图转换为字节 []?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/268013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert a Bitmap to byte[]?
提问by the-undefined
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.
基本上,我使用 listviews 插入事件插入图像,尝试从文件上传控件调整图像大小,然后使用 LINQ 将其保存在 SQL 数据库中。
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.
我找到了一些代码来创建文件上传控件中内容的新位图,但这是将它存储在服务器上的文件中,来自此源,但我需要将位图保存回 SQL 数据库,我认为我需要转换回 byte[] 格式。
So how do I convert the bitmap to a byte[] format?
那么如何将位图转换为 byte[] 格式呢?
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();
采纳答案by Eoin Campbell
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
};
回答by Tamir
Assuming, that your bitmap is bmp
假设你的位图是 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();
}
回答by Jon Skeet
If you've got a MemoryStream
already, just call MemoryStream.ToArray
to get the data out.
如果您已经有一个MemoryStream
,只需致电MemoryStream.ToArray
以获取数据。