简体   繁体   English

未在image.source中显示以使用转换器

[英]Not pictured in image.source to use converter

I have an Image that is bound as follows: 我有一个图像绑定如下:

<Image x:Name="ImgFoto" Source="{Binding Path=Foto, 
                      Converter={StaticResource imageConverter}, Mode=TwoWay, 
                      UpdateSourceTrigger=PropertyChanged}" Stretch="Fill" 
                      HorizontalAlignment="Left" 
                      Height="148" Margin="25,23,0,0" VerticalAlignment="Top" 
                      Width="193"/>"

"Foto" is a SQL field of type Image “ Foto”是图像类型的SQL字段

My converter class: 我的转换器课程:

public sealed class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        try
        {
            return new BitmapImage(new Uri((string)value));
        }
        catch
        {
            return new BitmapImage();
        }
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

My problem is that when you run the application the image appears blank. 我的问题是,当您运行应用程序时,图像显示为空白。

SQL Image type is in binary so you have to convert binary to bitmapimage instead of uri soirce. SQL映像类型为二进制,因此您必须将二进制转换为bitmapimage而不是uri soirce。 try below converter for that 尝试下面的转换器

public sealed class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        if (value == null || value.Length == 0) return null;
       var image = new BitmapImage();
       using (var mem = new MemoryStream((byte[])value))
       {
           mem.Position = 0;
           image.BeginInit();
           image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
           image.CacheOption = BitmapCacheOption.OnLoad;
           image.UriSource = null;
           image.StreamSource = mem;
           image.EndInit();
       }
       image.Freeze();
       return image;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

"Foto" is a SQL field of type Image “ Foto”是图像类型的SQL字段

If it is a field of type image, then it is stored in Binary format and in the converter, you are converting it to string. 如果它是图像类型的字段,那么它将以二进制格式存储,并且在转换器中,您正在将其转换为字符串。 This will never work because it is in Binary format. 这将永远不会起作用,因为它是二进制格式。 You need to get the bytes and convert it into Bitmap and then return it. 您需要获取字节并将其转换为Bitmap,然后将其返回。

Also if you don't intend to change the image, please make it OneWay binding. 另外,如果您不想更改图像,请使其成为OneWay绑定。

My new XAML is: 我的新XAML是:

            <Image x:Name="ImgFoto" Source="{Binding Foto}"  Stretch="Fill" HorizontalAlignment="Left" 
                       Height="148" Margin="25,23,0,0" VerticalAlignment="Top" Width="193"/>

My code for Charge Image: 我的收费图片代码:

  private void BtnCargarFoto_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog OD = new OpenFileDialog();
        OD.Filter = "jpg(*.jpg)|*.jpg|png(*.png)|*.png|gif(*.gif)|*.gif|bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
        if (OD.ShowDialog() == true)
        {
            using (Stream stream = OD.OpenFile())
            {
                bitCoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                ImgFoto.Source = bitCoder.Frames[0];
            }
            System.IO.FileStream fs = new System.IO.FileStream(OD.FileName, System.IO.FileMode.Open);
            foto = new byte[Convert.ToInt32(fs.Length.ToString())];
            fs.Read(foto, 0, foto.Length);
        }
    }

The problem: 问题:

The binding is excellent. 绑定是优秀的。 The problem now is that when charge an image on a record, the same image appears in all other records. 现在的问题是,对记录中的图像充电时,同一图像出现在所有其他记录中。 After loading and saving the image as you review the records through a Listview, the Image.Source is the same for everyone. 在通过列表视图查看记录并加载并保存图像后,每个人的Image.Source都是相同的。 I have to exit and reenter the application for the Image.Source to refresh properly. 我必须退出并重新输入该应用程序,Image.Source才能正确刷新。 Thanks a lot. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM