Why don't I see my programmatically generated image in WPF?
up vote
-1
down vote
favorite
I need to generate the image programmatically, so using an tag in the xaml isn't an option. I tried also to run at, when the window is loaded, but
Running the program just gives me a blank screen:
My .CS
namespace WpfApplication2
public partial class MainWindow : Window
public Image anImage;
public MainWindow()
InitializeComponent();
anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
this.AddVisualChild(anImage);
this.InvalidateVisual();
My XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Width="1280" Height="1024">
</Grid>
</Window>
c# wpf
|
show 3 more comments
up vote
-1
down vote
favorite
I need to generate the image programmatically, so using an tag in the xaml isn't an option. I tried also to run at, when the window is loaded, but
Running the program just gives me a blank screen:
My .CS
namespace WpfApplication2
public partial class MainWindow : Window
public Image anImage;
public MainWindow()
InitializeComponent();
anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
this.AddVisualChild(anImage);
this.InvalidateVisual();
My XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Width="1280" Height="1024">
</Grid>
</Window>
c# wpf
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
4
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
1
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44
|
show 3 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need to generate the image programmatically, so using an tag in the xaml isn't an option. I tried also to run at, when the window is loaded, but
Running the program just gives me a blank screen:
My .CS
namespace WpfApplication2
public partial class MainWindow : Window
public Image anImage;
public MainWindow()
InitializeComponent();
anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
this.AddVisualChild(anImage);
this.InvalidateVisual();
My XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Width="1280" Height="1024">
</Grid>
</Window>
c# wpf
I need to generate the image programmatically, so using an tag in the xaml isn't an option. I tried also to run at, when the window is loaded, but
Running the program just gives me a blank screen:
My .CS
namespace WpfApplication2
public partial class MainWindow : Window
public Image anImage;
public MainWindow()
InitializeComponent();
anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
this.AddVisualChild(anImage);
this.InvalidateVisual();
My XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Width="1280" Height="1024">
</Grid>
</Window>
c# wpf
c# wpf
edited Jun 4 '14 at 13:00
Anthony Russell
6,68874382
6,68874382
asked Jun 4 '14 at 12:37
wischi
112
112
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
4
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
1
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44
|
show 3 more comments
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
4
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
1
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
4
4
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
1
1
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
3
down vote
There are at least two things wrong with your code.
First, you should make sure that the image file can be loaded at runtime. The easiest way is to add the file to your Visual Studio project, perhaps in a folder named "Images". The Build Action of the file has to be set to Resource, but this is the default for image files anyway. Now you can access this image resource file in code behind by means of a Pack URI (or a Resource Pack URI to be more precise). Given the folder name "Images", that URI would look like this:
pack://application:,,,/Images/balloonB.png
Second, you should not use AddVisualChild to add the Image control to your MainWindow. Instead, add it to the top-level Grid by setting the x:Name attribute on the Grid in XAML, and then using the auto-generated member variable with that name in code-behind.
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
Add the Image control to the Grid:
rootGrid.Children.Add(anImage);
Putting it all together and omitting unnecessary property settings, your code would look like this:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
Or, a little shorter:
var anImage = new Image
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
;
rootGrid.Children.Add(anImage);
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
add a comment |
up vote
1
down vote
I find many times that a full pack Uri is a bit much and that a simplified Uri works just as well if not better, just be sure that your image build property is configured as MSBuild Resource:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean withgr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.
– Clemens
Jun 4 '14 at 16:48
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
|
show 3 more comments
up vote
0
down vote
There might be a problem with this line:
anImage.Margin = new Thickness(500, 500, 0, 0);
I renamed my Grid to gr and tried the following code and it works, I used the ImageBrush to draw the image:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assignanImage.Source = bmImage;.
– Clemens
Jun 4 '14 at 16:09
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
There are at least two things wrong with your code.
First, you should make sure that the image file can be loaded at runtime. The easiest way is to add the file to your Visual Studio project, perhaps in a folder named "Images". The Build Action of the file has to be set to Resource, but this is the default for image files anyway. Now you can access this image resource file in code behind by means of a Pack URI (or a Resource Pack URI to be more precise). Given the folder name "Images", that URI would look like this:
pack://application:,,,/Images/balloonB.png
Second, you should not use AddVisualChild to add the Image control to your MainWindow. Instead, add it to the top-level Grid by setting the x:Name attribute on the Grid in XAML, and then using the auto-generated member variable with that name in code-behind.
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
Add the Image control to the Grid:
rootGrid.Children.Add(anImage);
Putting it all together and omitting unnecessary property settings, your code would look like this:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
Or, a little shorter:
var anImage = new Image
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
;
rootGrid.Children.Add(anImage);
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
add a comment |
up vote
3
down vote
There are at least two things wrong with your code.
First, you should make sure that the image file can be loaded at runtime. The easiest way is to add the file to your Visual Studio project, perhaps in a folder named "Images". The Build Action of the file has to be set to Resource, but this is the default for image files anyway. Now you can access this image resource file in code behind by means of a Pack URI (or a Resource Pack URI to be more precise). Given the folder name "Images", that URI would look like this:
pack://application:,,,/Images/balloonB.png
Second, you should not use AddVisualChild to add the Image control to your MainWindow. Instead, add it to the top-level Grid by setting the x:Name attribute on the Grid in XAML, and then using the auto-generated member variable with that name in code-behind.
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
Add the Image control to the Grid:
rootGrid.Children.Add(anImage);
Putting it all together and omitting unnecessary property settings, your code would look like this:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
Or, a little shorter:
var anImage = new Image
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
;
rootGrid.Children.Add(anImage);
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
add a comment |
up vote
3
down vote
up vote
3
down vote
There are at least two things wrong with your code.
First, you should make sure that the image file can be loaded at runtime. The easiest way is to add the file to your Visual Studio project, perhaps in a folder named "Images". The Build Action of the file has to be set to Resource, but this is the default for image files anyway. Now you can access this image resource file in code behind by means of a Pack URI (or a Resource Pack URI to be more precise). Given the folder name "Images", that URI would look like this:
pack://application:,,,/Images/balloonB.png
Second, you should not use AddVisualChild to add the Image control to your MainWindow. Instead, add it to the top-level Grid by setting the x:Name attribute on the Grid in XAML, and then using the auto-generated member variable with that name in code-behind.
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
Add the Image control to the Grid:
rootGrid.Children.Add(anImage);
Putting it all together and omitting unnecessary property settings, your code would look like this:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
Or, a little shorter:
var anImage = new Image
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
;
rootGrid.Children.Add(anImage);
There are at least two things wrong with your code.
First, you should make sure that the image file can be loaded at runtime. The easiest way is to add the file to your Visual Studio project, perhaps in a folder named "Images". The Build Action of the file has to be set to Resource, but this is the default for image files anyway. Now you can access this image resource file in code behind by means of a Pack URI (or a Resource Pack URI to be more precise). Given the folder name "Images", that URI would look like this:
pack://application:,,,/Images/balloonB.png
Second, you should not use AddVisualChild to add the Image control to your MainWindow. Instead, add it to the top-level Grid by setting the x:Name attribute on the Grid in XAML, and then using the auto-generated member variable with that name in code-behind.
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
Add the Image control to the Grid:
rootGrid.Children.Add(anImage);
Putting it all together and omitting unnecessary property settings, your code would look like this:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
Or, a little shorter:
var anImage = new Image
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
;
rootGrid.Children.Add(anImage);
edited Nov 10 at 10:22
answered Jun 4 '14 at 17:11
Clemens
87.2k884166
87.2k884166
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
add a comment |
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
OK, it works, the point I missed was that I didn't put the image file inside the projectmap.
– wischi
Jun 5 '14 at 9:32
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
@wischi Then you should accept the answer (by clicking the check mark on the left below the vote counter). That would mark the problem as solved.
– Clemens
Jun 5 '14 at 10:43
add a comment |
up vote
1
down vote
I find many times that a full pack Uri is a bit much and that a simplified Uri works just as well if not better, just be sure that your image build property is configured as MSBuild Resource:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean withgr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.
– Clemens
Jun 4 '14 at 16:48
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
|
show 3 more comments
up vote
1
down vote
I find many times that a full pack Uri is a bit much and that a simplified Uri works just as well if not better, just be sure that your image build property is configured as MSBuild Resource:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean withgr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.
– Clemens
Jun 4 '14 at 16:48
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
|
show 3 more comments
up vote
1
down vote
up vote
1
down vote
I find many times that a full pack Uri is a bit much and that a simplified Uri works just as well if not better, just be sure that your image build property is configured as MSBuild Resource:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
I find many times that a full pack Uri is a bit much and that a simplified Uri works just as well if not better, just be sure that your image build property is configured as MSBuild Resource:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
edited Jun 4 '14 at 17:23
answered Jun 4 '14 at 13:46
Moez Rebai
3,68521738
3,68521738
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean withgr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.
– Clemens
Jun 4 '14 at 16:48
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
|
show 3 more comments
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean withgr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.
– Clemens
Jun 4 '14 at 16:48
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
This doesn't answer the question. Did you run the example code?
– Gusdor
Jun 4 '14 at 14:00
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
-1 for repeating the AddVisualChild and InvalidateVisual garbage. Fix that and provide a few more details about image resource files and Pack URIs (perhaps with this link), and I'll be happy to remove my downvote.
– Clemens
Jun 4 '14 at 16:20
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
Ok i edit it and i ll be glad for @wischi if you could help him with answer -_-
– Moez Rebai
Jun 4 '14 at 16:31
1
1
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean with
gr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.– Clemens
Jun 4 '14 at 16:48
Why don't you just show what it is? OP is apparently a WPF newbie, and I'm pretty sure that to him it is not that clear as to you what you mean with
gr. Maybe it would it be ok for you if I edit your answer? It's only a few but important points missing.– Clemens
Jun 4 '14 at 16:48
1
1
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
You wrote exactly one sentence that honestly sounds a little confusing. The rest is sample code and XAML. No explanations at all. Really a poor answer. However, I removed my downvote because you managed to get rid of the AddVisualChild nonsense.
– Clemens
Jun 4 '14 at 17:32
|
show 3 more comments
up vote
0
down vote
There might be a problem with this line:
anImage.Margin = new Thickness(500, 500, 0, 0);
I renamed my Grid to gr and tried the following code and it works, I used the ImageBrush to draw the image:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assignanImage.Source = bmImage;.
– Clemens
Jun 4 '14 at 16:09
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
add a comment |
up vote
0
down vote
There might be a problem with this line:
anImage.Margin = new Thickness(500, 500, 0, 0);
I renamed my Grid to gr and tried the following code and it works, I used the ImageBrush to draw the image:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assignanImage.Source = bmImage;.
– Clemens
Jun 4 '14 at 16:09
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
add a comment |
up vote
0
down vote
up vote
0
down vote
There might be a problem with this line:
anImage.Margin = new Thickness(500, 500, 0, 0);
I renamed my Grid to gr and tried the following code and it works, I used the ImageBrush to draw the image:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
There might be a problem with this line:
anImage.Margin = new Thickness(500, 500, 0, 0);
I renamed my Grid to gr and tried the following code and it works, I used the ImageBrush to draw the image:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
Xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
edited Jun 4 '14 at 17:25
Moez Rebai
3,68521738
3,68521738
answered Jun 4 '14 at 13:13
Claudio P
1,73521536
1,73521536
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assignanImage.Source = bmImage;.
– Clemens
Jun 4 '14 at 16:09
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
add a comment |
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assignanImage.Source = bmImage;.
– Clemens
Jun 4 '14 at 16:09
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
It really worked on yours? I copied your lines just below InitializeComponents(); inside the public MainWindow() and see no image still, also tried with this.InvalidateVisuals();
– wischi
Jun 4 '14 at 14:00
The ImageBrush is nonsense. Just assign
anImage.Source = bmImage;.– Clemens
Jun 4 '14 at 16:09
The ImageBrush is nonsense. Just assign
anImage.Source = bmImage;.– Clemens
Jun 4 '14 at 16:09
1
1
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
@Claudio You should also show the XAML with the named Grid, as OP might not know what you mean.
– Clemens
Jun 4 '14 at 16:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24037654%2fwhy-dont-i-see-my-programmatically-generated-image-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does the output window in visual studio has any info?
– Samuel
Jun 4 '14 at 12:40
set the window content instead of adding the visual. (this.Content = anImage;) However that will negate your grid. Otherwise add the image to the grid's children (after giving the grid a name).
– Jeff
Jun 4 '14 at 12:42
4
Firstly, welcome to Stack Overflow. Secondly, some more details... some more details... some more details... some more details... doesn't cut it here. Please edit your question, removing the nonsense and adding a proper description of your problem. Please also read through the Stack Overflow Help Centre to see how else you can improve your question and therefore, your likelihood of receiving a decent answer.
– Sheridan
Jun 4 '14 at 12:42
No, the output window has no comment, and this.Content has no effect. Every example I looked up in the web is using an <image ...> tag inside the <Grid ...> in the xaml, maybe generating an image programmatically didn't work? And Yes, this is my first C# program...
– wischi
Jun 4 '14 at 12:49
1
@Gusdor Do you really think that programming the WPF Visual Layer is the right suggestion for an apparent WPF newbie?
– Clemens
Jun 4 '14 at 16:44