AB4D Forum

Full Version: Help with ImageBrush
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I see how ImageBrush is used in Xaml, but I'm not having luck figuring out how paint a texture from an image file from a .cs file.

And while I'm asking, I have a folder of image files that I'd like to use in the application. Is the constructor 'public class ObjFileToWpfModel3DConverter'
with the Property 'Textures Directory' a way to accomplish this and if so, can you show the code to do this in a .cs file?

Thanks in advance for your help.
Loading bitmaps from code can be a little bit tricky because WPF uses a so called "Pack URI" schemas.

You can read more about that here: https://msdn.microsoft.com/en-us/library....100).aspx

But for your convenience here are the examples on how to read bitmap images and set them as ImageBrush and DiffuseMaterial.

// Load image from Resources folder with image's Build action set to Resource:
    var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Resources/PowerToysTexture.png", UriKind.RelativeOrAbsolute));

    // Load image from another assembly (ReferencedAssembly) from its Resources folder with image's Build action set to Resource:
    bitmapImage = new BitmapImage(new Uri("pack://application:,,,/ReferencedAssembly;component/Resources/PowerToysTexture.png", UriKind.RelativeOrAbsolute));

    // Load image from file
    bitmapImage = new BitmapImage(new Uri(@"D:\Images\myImage.png", UriKind.RelativeOrAbsolute));

    // Load image from disk from a loaction relative to the current location (site of origin)
    bitmapImage = new BitmapImage(new Uri("pack://siteoforigin:,,,/Resources/PowerToysTexture.png", UriKind.RelativeOrAbsolute));

    // Same as before but with providing full path to file
    bitmapImage = new BitmapImage(new Uri(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources/PowerToysTexture.png"), UriKind.RelativeOrAbsolute));

    // After you have the BitmapImage, yuo can create ImageBrush
    // ImageBrush allows you to set image tiling or specifying which part of the BitmapImage you want to show
    var imageBrush = new ImageBrush(bitmapImage);

    // Create DiffuseMaterial
    Box1.Material = new DiffuseMaterial(imageBrush);

Your second question is probably related to reading obj files that have textures defined in a directory that is different from the directory where the obj file is. In this case I would advice you to use ReadModel3D with an overload that also gets texturesDirectory:

var objImporter = new Ab3d.ReaderObj();
var wpf3DModel = objImporter.ReadModel3D(objFileName, texturesDirectory, defaultMaterial);
I have successfully drawn a Model3DFactory.CreateCylinder using an imagebrush using the 'siteoforigin' method for sourcing the image file and it draws the cylinder painted with the image exactly as I expected.

However, when I use the exact same code with the Mesh3DFactory.CreateExtrudedMeshGeometry method, I no longer see my extruded geometry. My code:
private Model3D DrawSegment()
            GeometryModel3D gm = new GeometryModel3D();
            ImageBrush colors_brush = new ImageBrush();
            colors_brush.ImageSource = new BitmapImage(new Uri("pack://siteoforigin:,,,/images/image.jpg", UriKind.RelativeOrAbsolute));
            DiffuseMaterial material = new DiffuseMaterial(colors_brush);
            gm.Material = material;
            gm.Geometry = Mesh3DFactory.CreateExtrudedMeshGeometry(_points, isSmooth, modelOffset, extrudeVector, shapeYVector);
            return gm;

If I use a DiffuseMaterial with a solid color, everything draws correctly. Is it possible that ExtrudedMeshGeometry doesn't support an image brush or do I have a syntax error?
The reason why the textures were not seen, is that extrude method did not yet create texture coordinates.

I have implemented that so that in the next version it will be possible to choose how the texture coordinates are generated.

A few days ago I have sent link to pre-release version to lloyd32552. If you also need that functionality, let me know and I send you instructions on how to get the pre-release version.