C#将图像转换为BLOB并将结果存储在文本框中
我正在做一个非常简单的项目。它需要 (1) 。允许用户选择图像文件,然后将其转换为可以作为BLOB存储在数据库中的格式 (2) 。将BLOB数据输出到文本框中 (3) 。框中输出的文本需要能够存储到数据库中,然后成功地转换回图像(此转换在别处处理) 这里的这个应用程序只是进行初始转换(图像到BLOB),以便用户可以将图像插入SQL数据库。但是,每当我运行程序时,每当我尝试打开文件时,它都会“冻结”。我做错了什么?有没有更有效的方法来完成我正在尝试的操作 多谢C#将图像转换为BLOB并将结果存储在文本框中,c#,image,visual-studio,blob,C#,Image,Visual Studio,Blob,我正在做一个非常简单的项目。它需要 (1) 。允许用户选择图像文件,然后将其转换为可以作为BLOB存储在数据库中的格式 (2) 。将BLOB数据输出到文本框中 (3) 。框中输出的文本需要能够存储到数据库中,然后成功地转换回图像(此转换在别处处理) 这里的这个应用程序只是进行初始转换(图像到BLOB),以便用户可以将图像插入SQL数据库。但是,每当我运行程序时,每当我尝试打开文件时,它都会“冻结”。我做错了什么?有没有更有效的方法来完成我正在尝试的操作 多谢 using System; usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Forms;
using System.IO;
namespace Binary_Converter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private OpenFileDialog imageDialog = new OpenFileDialog();
private FileStream imageStream;
public MainWindow()
{
InitializeComponent();
imageDialog.InitialDirectory = "c://";
imageDialog.Filter = "Image Files | *.jpg; *.gif; *.png";
imageDialog.FileOk += imageDialog_FileOk;
}
private void UI_Loaded(object sender, RoutedEventArgs e)
{}
void imageDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if((imageStream = (FileStream)imageDialog.OpenFile()) != null) {
byte[] buffer;
using(imageStream) {
buffer = new byte[imageStream.Length];
imageStream.Read(buffer, 0, (int)imageStream.Length);
}
foreach(byte i in buffer) {
outputText.Text += buffer[i];
}
}
}
private void addFileButton_Click(object sender, RoutedEventArgs e)
{
imageDialog.ShowDialog();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Windows.Controls;
使用System.Windows.Forms;
使用System.IO;
名称空间二进制转换器
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
私有OpenFileDialog imageDialog=新建OpenFileDialog();
私有文件流;
公共主窗口()
{
初始化组件();
imageDialog.InitialDirectory=“c://”;
imageDialog.Filter=“图像文件|*.jpg;*.gif;*.png”;
imageDialog.FileOk+=imageDialog\u FileOk;
}
已加载私有无效UI_(对象发送方、路由目标方)
{}
void imageDialog_FileOk(对象发送方,System.ComponentModel.CancelEventArgs e)
{
如果((imageStream=(FileStream)imageDialog.OpenFile())!=null){
字节[]缓冲区;
使用(图像流){
缓冲区=新字节[imageStream.Length];
读取(缓冲区,0,(int)imageStream.Length);
}
foreach(缓冲区中的字节i){
outputText.Text+=缓冲区[i];
}
}
}
私有void addFileButton_单击(对象发送方,路由目标)
{
imageDialog.ShowDialog();
}
}
}
我认为您的程序冻结是因为您没有将值转换为十六进制格式。您试图输出原始字节值,这会“损害”您的文本框,因为它会将其解释为带有控制字符的字符串。如果要将值插入数据库,则需要十六进制格式
foreach(byte i in buffer) {
outputText.Text += buffer[i];
}
outputText.Text = "0x"; // begin the string with 0x to tell that it is hexadecimal
foreach(byte i in buffer) {
outputText.Text += buffer[i].ToString("x2"); // convert each byte to hexadecimal form
}
您不需要将其传递到文本框,然后将其保存到数据库。 如果您有一个
图像
或Blob
列,则直接保存字节[]
本文()提供了有关这方面的更多信息