0

I put my pictures of letters in a separate table and I defined an foriegn key for that I now want to click on any data in the data grid view to show the photos of that level in the control of the image. I wrote the order for the retrieval of the photos, Of course, I do not know if it's right and I do not know how to control the image. code

        SqlConnection imageConnection = new SqlConnection(@"Data Source = ZAHRA; Initial Catalog = Baygani; inte");

        SqlCommand imageCommand = new SqlCommand(@"select Pictures.pic1,Pictures.pic2,Pictures.pic3,Pictures.pic4,Pictures.pic5,Pictures.pic6,Pictures.pic7,Pictures.pic8,Pictures.pic9,
         Pictures.pic10 from Pictures,Latters where Pictures.idlatter=Latters.id ", imageConnection);

        imageConnection.Open();
        SqlDataReader imageReader = imageCommand.ExecuteReader();

        string imageFilename = (string)imageReader.GetValue(0);
        byte[] imageBytes = (byte[])imageReader.GetValue(1);
        MemoryStream ms = new MemoryStream(imageBytes);
1
  • 1
    Assign the MemoryStream to the StreamSource property of a BitmapImage. Then assign the BitmapImage to the Source property of an Image element in your UI. Commented Nov 8, 2017 at 20:58

1 Answer 1

1

Assuming that you are using Bindings to display your data in your UI and thus using viewmodels, I would suggest utilizing an ImageSource.

To initialize an ImageSource from a byte array I wrote the following method:

public ImageSource LoadImageSourceFromBytes(byte[] imageBytes)
{
    BitmapImage bitmapImage = new BitmapImage();

    using (MemoryStream imageStream = new MemoryStream(imageBytes))
    {
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = imageStream;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
    }

    bitmapImage.Freeze();

    return bitmapImage;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain more where should I write it? I added this function, but still I do not know how to display the photos in image control?
The app is made with WPF
Do you use the MVVM Pattern in your Application? If so you have to put the Property in your ViewModel and from the View you use the Binding MarkupExtension to display the Image in the View. If not Assign a x:Name to the Image Control and in Code behind set the Source Property of that control to the Image you got from the Database. But I Highly recommend locking up MVVM (and use it) if you are not already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.