Bank image conversion to use in a background

2

I'm using the following code to put buttons with each category name, but I'm not getting (I do not know how to search) convert to background image, just give me the tostring option.

        {
            ctx.DefaultContainerName = "kinectEntities";

            Console.WriteLine("View criada.. parametro = " + id);

            //cria a query geral.. "select * from produtos"
            ObjectSet<produtos> query = ctx.CreateObjectSet<produtos>();

            //cria a query com o where "select * from produtos where id_categoria = ?"
            var query2 = query.Where("it.id_categorias = @categoria");

            //adiciona o parametro na query2 select * from produtos where id_categoria = 7"
            query2.Parameters.Add(new ObjectParameter("categoria", id));

            foreach (produtos r in query2)
            {
                //percorrendo registros da base
                var button = new KinectTileButton { Label = (r.valor).ToString(CultureInfo.CurrentCulture), Tag = r.id,
                --->Background = r.imagem.ToString()<----};
                button.Click += KinectTileButtonClick;
                this.wrapPanel.Children.Add(button);
            }
    
asked by anonymous 15.09.2017 / 19:25

2 answers

1

- For images saved in the database as Base64 -

I believe the image has been saved as a byte array (data format).

So, do this:

Place this method in some helper class or inside your code-behind or somewhere that you can easily access.

public static ImageBrush GetBrushFromImage(byte[] imageData)
{
   ImageBrush brush;
   BitmapImage bi;
   using (var ms = new MemoryStream(imageData))
   {
       brush = new ImageBrush();

       bi = new BitmapImage();
       bi.BeginInit();
       bi.CreateOptions = BitmapCreateOptions.None;
       bi.CacheOption = BitmapCacheOption.OnLoad;
       bi.StreamSource = ms;
       bi.EndInit();
   }

   brush.ImageSource = bi;
   return brush;
}

Then call it like this:

foreach (produtos r in query2)
{
   //percorrendo registros da base
   var button = new KinectTileButton { Label = (r.valor).ToString(CultureInfo.CurrentCulture), Tag = r.id, 
   Background = GetBrushFromImage(Convert.FromBase64String(r.Image))};
   button.Click += KinectTileButtonClick;
   this.wrapPanel.Children.Add(button);
}

- For images saved as URI (path to the resource) -

public static ImageBrush GetBrushFromImageUri(string uri)
{
   return new ImageBrush(new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute)));
}

Then call it like this:

foreach (produtos r in query2)
{
   //percorrendo registros da base
   var button = new KinectTileButton { Label = (r.valor).ToString(CultureInfo.CurrentCulture), Tag = r.id, 
   Background = GetBrushFromImageUri(r.Image)};
   button.Click += KinectTileButtonClick;
   this.wrapPanel.Children.Add(button);
}
    
15.09.2017 / 20:10
0

Resolved @MurariAlex , thanks for the help!

Here is the code I used to solve my problem:

public static ImageBrush GetBrushFromImageUri(string uri)
{
    string filePathRelativeToAssembly = Path.Combine(
        @"C:\Bitnami\wampstack-5.6.30-2\apache2\htdocs\mvc\public",
        uri
    );

    string normalizedPath = Path.GetFullPath(filePathRelativeToAssembly);

    return new ImageBrush(
        new BitmapImage(
            new Uri(normalizedPath,UriKind.RelativeOrAbsolute)
        )
    );
}
    
18.09.2017 / 19:38