C#如何在不使用文件对话框的情况下将图像保存到数据库中 [英] C# how can I save image into database without using file dialog

查看:64
本文介绍了C#如何在不使用文件对话框的情况下将图像保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用qrcode显示图像,但我不知道如何在sql server中保存它。



我尝试了什么:



这是我从qr到picturebox显示图像的方式:



byte [] imageBytes = dr [SmStockImage] as byte [];

if(imageBytes!= null)

{

using(var stream = new MemoryStream (imageBytes))

pbxBagImg.Image = Image.FromStream(stream);

}

Im using a qrcode to display image but i don't know how to save it in sql server.

What I have tried:

this is how i display image from qr to picturebox:

byte[] imageBytes = dr["SmStockImage"] as byte[];
if (imageBytes != null)
{
using (var stream = new MemoryStream(imageBytes))
pbxBagImg.Image = Image.FromStream(stream);
}

推荐答案

To获取图像的字节,执行以下操作。 注意:如果您不使用JPEG,请更改格式。



To get the bytes for the image, do the following. Note: if you're not using JPEG, change the format.

byte[] bytes;

using (var stream = new MemoryStream())
{
  image.Save(stream, ImageFormat.Jpeg);
  stream.Flush();

  bytes = stream.ToArray();
}







假定图片是您之前使用要保存的图像初始化的变量。



此后,您只需传递 bytes <的值/ code>作为 SQL INSERT 命令的参数。




This assumes image is a variable you have previously initialized with the image you want to save.

After this, you just pass the value of bytes as a parameter into your SQL INSERT command.


这篇关于C#如何在不使用文件对话框的情况下将图像保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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