12/26/2023 0 Comments Wpf transparent rectangleOddly if I explicitly add Canvas.Top="-15" into the XAML markup it DOES properly shift the offset to a negative position, but for some reason this behavior is not working in code any which way I tried. If ( ) ( Canvas.TopProperty, Radius * -1) īut it doesn't. Here's what the actual display Image code looks like: The code now has to size the canvas, the inner grid and the rectangle all to the same size before rendering. By wrapping the inside Grid in a Canvas of the same size the rendering started to work properly: So the margins of the Grid, which by default centers all content (which is what I want so the image is always centered in the view) are the issue here. So after a lot of back and forth to figure this out (and some help from Mark Miller of DevExpress) I finally figured out that the margins where being applied and applied a simple solution: Wrap the inner Grid into another container without any margins. The margins are implicit - I never declared them but they are nevertheless implied. So the background of the 'empty' image is transparent, not Azure which would have accounted for the parent container. What's happening is that WPF is rendering the visual control I provided but it's applying any margins that are applied against it. It looks like WPF is rendering the parent container - but actually it's not. It turns out that it was shifted exactly by the margins of the Parent container or in this case RenderContainer. After some more experimenting I found that the content actually wasn't blank but rather shifted off rather far to the bottom and right. In fact, the output generated where the correct size I specified in the form, but the content was - well blank. Simple enough (if you know what to do ).īut unfortunately this did not work. The bitmap is then added to a Bitmap Encoder which in turn can write out the bitmap to a file. RenderTargetBitmap handles the appropriate scaling to provide the correct size. The RenderBitmap object is used and it renders the target in system level pixels not WPF device independent pixels - important because if the bitmap is used anywhere but WPF you'll want standard fixed pixel sizes. This code is supposed to look at the above code and render it into a bitmap. Private void btnSave_Click( object sender, RoutedEventArgs e) With code that essentially does the following: The rectangle then has a Radius X and Radius Y assigned to create the rounder corners. So the idea is to draw the outer container and fill the container with the rectangle. There are some tricky issues to deal with in the shapes - like a Rectangle - aren't controls, but drawings so they don't act like a container. the parent grid) and then render a rectangle into it at the right size and I'm done. So I figured all I'd have to do is stick a Grid inside of the of the fill container (ie. The DockPanel handles the docking of the controls at the top and the save options on the bottom with the grid making up the fill of the rest of the window DockPanel content. The basic set up is this: The middle section of the form above is a Grid that lives inside of a DockPanel. So, I thought how hard could this be? Well it turns out the concept is very simple, but due to some very unexpected behavior in WPF it too a long time and some help from a friend to figure it out. Even nicer, WPF properly respects transparency when rendering to disk, something that's a real bitch in GDI+ when needing to save to GIF images. So the whole point of this exercise was to see how "easy" it'd be to do this because WPF provides the ability to take any rendered visual and lets render it to a bitmap. Trivial, but actually kinda useful - there are a few similar combinations I'm thinking would be useful: Creating gradients (easier and more trivial), creating shadow images etc. It renders in real time as you type so you see immediately what it looks like. The idea is you type in a width, height, radius and color and it will generate the appropriate rounded bar for you. A few hours later I got it working, but there were a few hiccups along the way. Oh ok, so I have tools that do that shit, but I wanted to see what it takes to do this with WPF. Since it's fairly easy to create images and shapes in WPF I thought I'd create a quick form to created rounded corner bars, because one can never have enogh of those in Web applications. One thing I like about WPF is the fact that you can take just about anything that you render on the canvas and dump it out as a bitmap file. I'm playing around a little today with WPF again.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |