How to save an image to SQL by using EF 4.1, MVVM?

Sep 10, 2011 at 9:47 AM

Hi,

<Border BorderBrush="LightGray" BorderThickness="2" Margin="4,10" Grid.Row="1" Grid.Column="4" Grid.RowSpan="4">
                                        <Image x:Name="vb" MouseLeftButtonUp="Image_MouseLeftButtonUp" Width="150">
                                            <Image.Source>
                                                <BitmapImage DecodePixelWidth="200" UriSource="C:\NVIDIA\bird 3.jpg" />
                                            </Image.Source>
                                        </Image>
                                    </Border>

 

I would like to open the dialogbox control then save to DB

private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.ShowDialog(); FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            //vb.Source
        }

Sep 11, 2011 at 5:50 AM
Edited Sep 11, 2011 at 5:53 AM

Hi,

I was solved it. Search from many time there's no help so that below was my code. Am I right?

private void SetImageExecute(object sender)
        {
            if ((string)sender != string.Empty)
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.ShowDialog();
                FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();

                var item = this.context.ArCodes.Find((string)sender);
                item.MyImage = data;
                this.RaisePropertyChanged(() => this.ActiveArCode);
            }

        }

 

Another Question I would like to changed to an alternative way. How to store images path in sql I know how to fix it in all web app but WPF does not work. I need to store relative path.

How?