|
I did exactly the same thing you did in your example. I put a .png file in a folder off the project called Images, made sure its BuildAction was Resource, then tried to look at the BitmapImage.PixelWidth (which I needed to size the window exactly that was to contain the image). I got an exception that reported that it "couldn't find part of the path", and it quoted a path something like, "C:\...\bin\Debug\Images\Airplane.png". Then when I set the image Source to the BitmapImage I got no exception and when I hovered over the image.Source code in the debugger, it showed a URI something like this:
"pack://application:,,,/MyProject;component/Images/Airplane.png
This is the syntax for an absolute pack URI when the resource is in a subfolder of a referenced assembly. But the image did not display in the window. I got nothing but a blank window.
Then I went back to an absolute URI. This time I was able to examine the PixelWidth of the BitmapImage with no exception and when I hovered over the image.Source in the debugger it showed this:
"pack://application:,,,/Images/Airplane.png"
This is the syntax for an absolute pack URI when the resource is in a subfolder of the local assembly. Continuing execution, I saw the image display in the window.
Any suggestions about how I could go about debugging what is wrong when I use a relative pack URI?
|
|
|
|
|
I'm not sure what's happening on your end, but you
shouldn't be getting any paths like "C:\...\bin\Debug\Images\Airplane.png",
since that is NOT a pack uri for extracting a resource image.
As far as getting PixelWidth/PixelHeight, that could be tricky.
For example, with the following code:
myimage.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(myimage_ImageFailed);
Uri uri = new Uri("/Images/Airplane.png", UriKind.RelativeOrAbsolute);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = uri;
bi.EndInit();
myimage.Source = bi;
with a breakpoint on "myimage.Source = bi;", bi.PixelWidth/bi.PixelHeight
are 0, and also having the breakpoint there causes the image to not appear.
Put a breakpoint after "myimage.Source = bi;" (removing the
previous breakpoint!) and bi.PixelWidth/bi.PixelHeight are valid (but
is that just lucky timing?).
It appears the decoding is asynchronous, and I see no event for
decoding completion...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Your statement about getting pixel dimensions could be tricky was all the hint I needed. The problem was I was executing the following statement last in my Loaded event:
image.Source = screenshot;
and immediately after the following statement
BitmapImage screenshot = new BitmapImage(uri);
I was getting the pixel dimensions of the image:
double imageWidth = 450;
double imageHeight = 300;
try
{
imageWidth = screenshot.PixelWidth;
imageHeight = screenshot.PixelHeight;
}
catch (Exception ex)
{
string error = ex.Message;
}
Apparently the very act of asking for the pixel dimensions somehow corrupted the screenshot BitmapImage object. When I moved image.Source = screenshot; from the end of the event to the statement immediately following the BitmapImage constructor, UriKind.Relative started working! Apparently using an absolute pack URI provided enough of a delay to complete the asynchronous decoding.
I hope this explanation is clear. If it isn't, I could make another post showing all of the code for the Loaded event. Thank you very much for sticking with me on this issue.
|
|
|
|
|
Glad you got it working.
I still hope they add a proper event like they did for
Silverlight, in which they added ImageOpened events to both
the BitmapImage and Image classes, specifically to get a look
at the loaded bitmap's dimensions before rendering.
Getting lucky with timing worries me, although in this case
maybe there's some mechanism forcing the decoding of the image
at that instance. I haven't seen that confirmed in any docs though
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yeah, it's pretty important to be able to get the image dimensions reliably. I do that all over the place, but I'm usually not dealing with assembly resources but external files.
|
|
|
|
|
<DockPanel LastChildFill="True" Height="18">
<Image Height="18" Width="80">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing Brush="Black">
<GeometryDrawing.Geometry>
<LineGeometry StartPoint="0,9" EndPoint="38,9" />
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen DashCap="Flat"
Brush="Black"
Thickness="0.5"
DashStyle="{x:Static DashStyles.Dot}, Mode=OneTime}"
/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
<TextBlock Height="18" Text="{Binding Name, Mode=OneTime}" Margin="3,2,5,0" />
</DockPanel>
Actually, I want to list all line styles in a ComboBox.
Glad to discuss with you and best wishes.
|
|
|
|
|
See this discussion: DashStyles.Dot Bug?[^]
(try changing your DashCap to Square or Round)
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello Friends,
Is there any limitation on the number of controls that can be used in a single wpf window?
Thanking in advance
Johnny
|
|
|
|
|
I don't think there's been a control count limit since we all escaped from VB6, and even there it was possible to get around it with control arrays.
So basically, you're limited by your computer and your patience, because the more controls you have, the longer it'll take to render.
|
|
|
|
|
I'm working on learning WPF, and I'm having an issue with my menu. The menu displays fine but when I click on a root level item, the sub menu does not appear to drop down. It still appears that I can click on where the menu item should be, and it will fire the proper event. Is there something common errors that cause this or do i need to post some source code because I'm doing something dumb?
Thanks.
Ennis Ray Lynch, Jr. wrote: Unpaid overtime is slavery.
Trollslayer wrote: Meetings - where minutes are taken and hours are lost.
|
|
|
|
|
Shelby Robetson wrote: do i need to post some source code
The sample code in the WPF SDK[^] works fine for me...
What are you doing different?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
In terms of the menu itself, I'm not doing anything differently. I'll post the source when I get home this evening.
Ennis Ray Lynch, Jr. wrote: Unpaid overtime is slavery.
Trollslayer wrote: Meetings - where minutes are taken and hours are lost.
|
|
|
|
|

<Window x:Class="Test.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="500" Width="700" ResizeMode="CanResizeWithGrip" Name="MainWindow"
WindowStyle="None" AllowsTransparency="True"
Background="Transparent" BorderThickness="0" FrameworkElement.Loaded="MainWindow_Loaded" FrameworkElement.SizeChanged="Window_SizeChanged" >
<Window.Resources>
<DrawingBrush x:Key="yellowBackground">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush Center="0.54326,0.45465" RadiusX="0.402049" RadiusY="1.02049" GradientOrigin="0.4326,0.45465">
<GradientStop Color="#ffffe1" Offset="0"/>
<GradientStop Color="#f8e737" Offset="0.461"/>
<GradientStop Color="#e79b00" Offset="1"/>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0 1,1" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" SpreadMethod="Pad">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#39FFFFFF" Offset="1"/>
</LinearGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0 1,1" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Window.Resources>
<Border Background="LightSteelBlue" CornerRadius="20,20,20,20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="44.149" />
<RowDefinition Height="*"/>
<RowDefinition Height="150" MinHeight="20px"/>
</Grid.RowDefinitions>
<Border Height="44" VerticalAlignment="Top" CornerRadius="20,20,0,0" Background="{StaticResource yellowBackground}" Grid.Row="0">
<StackPanel Height="44" VerticalAlignment="Top" Orientation="Vertical">
<Canvas Height="22" Background="Transparent" Name="TitleCanvas" PreviewMouseLeftButtonDown="DragWindow">
<Label FontWeight="Bold" FontFamily="Elephant" FontStyle="Italic" HorizontalAlignment="Left" Grid.Column="0">Test Title</Label>
</Canvas>
<Menu Height="22" Background="Transparent" BorderThickness="0" >
<MenuItem Header="File">
<MenuItem Header="Exit" Click="Exit_Click" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="About..." />
</MenuItem>
</Menu>
</StackPanel>
</Border>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MinWidth="25" MaxWidth="{Binding ElementName=MainWindow, Path=ActualWidth}"/>
<ColumnDefinition Width="*" MinWidth="25"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" x:Name="NavPanel">
<TreeView x:Name="NavTree" Background="Transparent" Height="{Binding ElementName=NavPanel, Path=ActualHeight}" BorderThickness="0"></TreeView>
</StackPanel>
<GridSplitter Width="3px"></GridSplitter>
<StackPanel x:Name="lvPanel" Grid.Column="1" Background="Transparent">
<ListView Name="listView1" Width="{Binding ElementName=lvPanel, Path=ActualWidth}" Height="{Binding ElementName=lvPanel, Path=ActualHeight}" BorderThickness="0" Background="Transparent"/>
</StackPanel>
</Grid>
<GridSplitter Grid.Row="2" ResizeDirection="Rows"
Grid.ColumnSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Height="3px"/>
</Grid>
</Border>
</Window>
Ennis Ray Lynch, Jr. wrote: Unpaid overtime is slavery.
Trollslayer wrote: Meetings - where minutes are taken and hours are lost.
|
|
|
|
|
Menu items show up fine here...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the help. I've got it working also, it appears the solution was "restart visual studio". I made no changes since the time I posted the initial question and it just works.
Ennis Ray Lynch, Jr. wrote: Unpaid overtime is slavery.
Trollslayer wrote: Meetings - where minutes are taken and hours are lost.
|
|
|
|
|
Hi,
I need to find the binding control of my TreeViewItem's parent, GetContainerFromItem gives me the parent TreeViewItem and GetParent gives me the Parent Item, but how do I get the control that is bound to the parent Item?
I'm using a custom control to display the items, and need to manipulate it from a child control...
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Hi,
In my silverlight application I've many pages(1,2,3...), after logged in i'm showing default page. After that if i click on any page default loading (silverlight progress like rotating a default logo with continual increment of time) is going on.
Next if i click on the any other page of same page that silverlight progress is not showing. My silverlight pages have huge data, so for loading the data it takes time to show any page without showing any progress.
I need to show any progress bar on loading the data.... is it possible to show the progress bar in all pages while loading?
Regards,
Ravi Vellanki.
|
|
|
|
|
ravi.vellanky wrote: is it possible to show the progress bar in all pages while loading?
Yes.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Can you tell me how can i achieve this?
|
|
|
|
|
What part are you having trouble with?
Data loading is asynchronous in Silverlight so you can
display a progress control at the start of the download
and remove the progress control when the download completes.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Can you give me any sample application link on this progress bar?
|
|
|
|
|
Have you looked at the documentation?
ProgressBar Class[^]
1) Before you request data, show a ProgressBar.
2) If you're using a data retrieval method that reports download status, update the ProgressBar.
If no download status is available, you could set the ProgressBar's IsIndeterminate property to True.
3) When the data is received, hide and/or remove the ProgressBar.
Again - what part are you having trouble with?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hey all
I added silverlight to an existing asp.net project and used asp:Silverlight control to embed the silverlight, everything works fine.
I set color of my website body to LightGray but the silverlight control background is still showing white, I tried setting the control backcolor to Transparent but still no luck.
Any help will be appresaited.
Thanks.
|
|
|
|
|
The background of the plugin object (what you see before
any XAML is loaded) is set on the plugin object as described
here: Background (Silverlight Plug-in Object)[^]
Note the default is "white", and if you use transparent, I'm pretty sure
you'll see black.
Once there's Silverlight elements up, those elements will determine
the background.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for your reply, it helped me alote
Mark Salsbery wrote: Note the default is "white", and if you use transparent, I'm pretty sure
you'll see black.
It did show a black background, i just set the windowless property to true, now it shows perfect.
Thanks again.
|
|
|
|