插入浏览图片wpf [英] insert browse image wpf

查看:60
本文介绍了插入浏览图片wpf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need to insert an image to MySql using WPF.
The image can be selected value of browse&choose button.
Only I don't have enough experience with browse&choose image insertion.
byte[] ImageBytes = (byte[])PredictedHelpNeededArea; 
Below is my code:
<Button x:Name="btnBrowse" Content="Browse" Click="btnBrowse_Click"/>
<Button x:Name="btnInsert" Content="Insert" Click="btnInsert_Click"/>
<Image Name="MyImage"></Image>
private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            {
                
                if (MyImage.Source != null)
                {
                    MySqlConnection con = new MySqlConnection(constr);
                    MySqlCommand cmd = new MySqlCommand("INSERT INTO images (Image_BLOB) VALUES (@ImageSource)", con);
                    byte[] ImageBytes = (byte[])PredictedHelpNeededArea;
                    cmd.Parameters.Add("@ImageSource", MySqlDbType.Blob, ImageBytes.Length).Value = ImageBytes;
                    con.Open();
                    int i = cmd.ExecuteNonQuery();
                    if (i > 0)
                    {
                        MessageBox.Show("Image Inserted");
                    }
                
                }
                else
                {
                    MessageBox.Show("Choose an image");
                }
            }
            
        }

推荐答案

看起来像您需要以下三个步骤的帮助:

It looks like you need help with these three steps:


  1. 用于选择图像的UI。
  2. 加载图像并将其放入在你的图像控件中。
  3. 将其转换为字节数组。



第一步,选择图像的UI,使用 Microsoft.Win32.OpenFileDialog class:


For the first step, the UI to select the image, use the Microsoft.Win32.OpenFileDialog class:

Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.Filter = "Image Files|*.jpg;*.png;*.gif"; // add more image formats if desired, semi-colon separated, format: *.<extension>
openFileDialog.CheckPathExists = true;
bool? result = openFileDialog.ShowDialog();
string path;
if (result.HasValue && result.Value)
{
    path = openFileDialog.FileName;
}
else
{
    // user did not select image
    return;
}
</extension>



要加载图像并将其放入图像控件中,您可以使用 BitmapImage class:


To load the image and put it in the image control, you can use the BitmapImage class:

BitmapImage source = new BitmapImage(new Uri(path));
MyImage.Source = source;



然后从此图像源获取字节。您可以使用 MemoryStream 和位图编码器:


Then get the bytes from this image source. You can use a MemoryStream and a bitmap encoder for this:

PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
byte[] ImageBytes;
using (MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    ImageBytes = ms.GetBuffer();
}



而不是 PngBitmapEncoder 您可以使用任何位图编码器,无论您想要什么。



现在您可以将 ImageBytes 保存到您的数据库。


Instead of PngBitmapEncoder you can use any bitmap encoder, whichever you want.

Now you can save ImageBytes to your database.


这篇关于插入浏览图片wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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