位图图像到字节[]

发布于 2024-11-18 17:56:32 字数 154 浏览 2 评论 0原文

我有一个在 WPF 应用程序中使用的 BitmapImage,稍后我想将其作为字节数组保存到数据库中(我想这是最好的方法),如何执行此转换?

或者,是否有更好的方法将 BitmapImage(或其任何基类、BitmapSource 或 ImageSource)保存到数据存储库?

I have a BitmapImage that I'm using in a WPF application, I later want to save it to a database as a byte array (I guess it's the best way), how can I perform this conversion?

Or, alternatively, is there a better way to save a BitmapImage (or any of its base classes, BitmapSource or ImageSource) to a data repository?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

客…行舟 2024-11-25 17:56:32

要转换为 byte[],您可以使用 MemoryStream:

byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}

您可以使用您喜欢的任何 BitmapEncoder,而不是 JpegBitmapEncoder,如 casperOne 所说。

如果您使用 MS SQL,您还可以使用image-Column,因为 MS SQL 支持该数据类型,但您仍然需要以某种方式转换 BitmapImage。

To convert to a byte[] you can use a MemoryStream:

byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}

Instead of the JpegBitmapEncoder you can use whatever BitmapEncoder you like as casperOne said.

If you are using MS SQL you could also use a image-Column as MS SQL supports that datatype, but you still would need to convert the BitmapImage somehow.

沦落红尘 2024-11-25 17:56:32

您必须使用派生自 BitmapEncoder(例如BmpBitmapEncoder)并调用 Save 方法将 BitmapSource 保存到 Stream

您可以根据要保存图像的格式选择特定的编码器。

You will have to use an instance of a class that derives from BitmapEncoder (such as BmpBitmapEncoder) and call the Save method to save the BitmapSource to a Stream.

You would choose the specific encoder depending on the format you want to save the image in.

初熏 2024-11-25 17:56:32

将其写入MemoryStream,然后您可以从那里访问字节。
有点像这样:

public Byte[] ImageToByte(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}

write it to a MemoryStream, then you can access the bytes from there.
something kinda like this:

public Byte[] ImageToByte(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}
风渺 2024-11-25 17:56:32

您可以给出位图的格式:

Image bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics.FromImage(bmp).CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);

MemoryStream m = new MemoryStream();
bmp.Save(m, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageBytes = m.ToArray();
string base64String = Convert.ToBase64String(imageBytes);

You can give a formate of bitmap:

Image bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics.FromImage(bmp).CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);

MemoryStream m = new MemoryStream();
bmp.Save(m, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageBytes = m.ToArray();
string base64String = Convert.ToBase64String(imageBytes);

樱花坊 2024-11-25 17:56:32

只需使用 MemoryStream 即可。


byte[] data = null;

using(MemoryStream ms = new MemoryStream())
{
    bitmapImage.Save(ms);
    data = ms.ToArray();
}


Just use a MemoryStream.


byte[] data = null;

using(MemoryStream ms = new MemoryStream())
{
    bitmapImage.Save(ms);
    data = ms.ToArray();
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文