WPF: put an icon on a radWindow programatically - wpf

I want to put an icon on the top left of a radWindow programatically
my code is like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("../ressources/enregistrer.png", UriKind.Relative))
};
radWindow.Show();
but the icon dont show up
does anyone have an idea ?
EDIT
This is the architecture on my project:
The file from where the above code is taken is circled in red
The ressource file is circled in green

<telerik:RadWindow.Icon>
<Image Source="pack://application:,,,/ressources/enregistrer.png" Height="18"/>
</telerik:RadWindow.Icon>
// this seems to work. You will need to provide a size.

I dont know why my solution above did not work but I found a workaround like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("pack://application:,,,/ressources/enregistrer.png", UriKind.RelativeOrAbsolute))
}
radWindow.Show();

Related

Printing In Silverlight. Some pages missing

I am maintaining an old application that prints checks from silverlight.
The checks are in a grid and the user selects them and presses the print button.
I verified that all the checks selected in the grid do get sent to the printer but I noticed that sometimes some are missing in the actual printout. I check the EndPrint even for errors and there is none.
How can I make sure all the data gets actually printed?
Here is the code for the printpage event
StackPanel stackPanel = new StackPanel();
CheckInfo check = selectedChecks[printItemIndex];
PrintCheck printCheck = BuildPrintCheck(check);
stackPanel.Children.Add(printCheck);
stackPanel.Measure(new Size(args.PrintableArea.Width, double.PositiveInfinity));
if (++printItemIndex < selectedChecks.Count)
args.HasMorePages = true;
args.PageVisual = stackPanel;
I found a workaround to this issue posted here
http://www.thomasclaudiushuber.com/blog/2009/11/25/how-to-print-dynamically-created-images-in-silverlight-4-beta/
Basically instead of putting the Image straight into the page, put a Rectangle and and at run time load the image dynamically, set it as the image source for an image brush and then set the fill property of the rectangle to the image brush.
OK it turned out that SilverLight5 (runtime) has an issue with printing images. The problem does not exist on clients running SilverLight4.
Here is how I fixed it in my code
private void PlaceImages()
{
var logoStreamResourceInfo = Application.GetResourceStream(new Uri("myApp;/Images/logo.png", UriKind.Relative));
var logo = new BitmapImage();
logo.SetSource(logoStreamResourceInfo.Stream);
var logoImageBrush = new ImageBrush();
logoImageBrush.ImageSource = logo;
upperLogo.Fill = logoImageBrush;
lowerLogo.Fill = logoImageBrush;
}

How to change the background of a grid to an image in WP7 silverlight?

I'm trying to set a background for a grid control in WP7 silverlight, I need to do that programatically, not in the desighn.
I tried something like:
ContentPanel.Background = new BitmapImage(new Uri("Images\Backgrounds\with box\13.jpg", UriKind.Relative));
But of course, I got an error, because on the right hand we should have the type SolidColorBrush.
is there a way to do that?
Thanks..
I would firstly recommend that you use a Canvas instead of a Grid. You can do this by deleting the Grid and inserting a Canvas through the Toolbox -> Drag and Drop.
Then, you can use the code as simple as:
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("url", UriKind.Relative));
CanvasName.Background = imageBrush;
That will change the background to whatever you want.
Hope that helps.

How to remove the border of a Button in code?

I am developing a windows phone 7 application. I am new to windows phone 7 applications. In my application I have created a button control dynamically and added the background image to the button control as follows.
Button AlphabetButton = new Button();
AlphabetButton.Content = vAlphabet;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
//brush.Stretch = Stretch.None;
AlphabetButton.Background = brush;
AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);
AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
AlphabetButton.Width = 80;
AlphabetButton.Height = 80;
I want to remove the border of the button control because with that border the image does not appear as per requirement. How to do this? Can we do this with the BorderThickness attribute of button control or is there any other way? Can you please provide me any code or link through which I can resolve the above issue? If I am doing anything wrong then please guide me.
The easiest way is to set the BorderThickness to 0 like for Example:
Button alphabetButton = new Button();
alphabetButton.BorderThickness = new Thickness(0.0);
Another option could be to set the BorderBrush to Transparent or to change the whole Style of the button and especially its ControlTemplate.
I think Button.StrokeTHickness is the correct property to adjust the borders.

Using a Windows Forms icon in WPF

I've got this WPF code which works...
Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);
I'm using a windows forms notifyIcon control in my WPF app, and I now need to assing the Icon to it. How do I get from my WPF icon to a System.Drawing.Icon ?
I use the following method:
// Setup the new NotifyIcon
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Text = "Name of Application";
notifyIcon.Icon = new System.Drawing.Icon("media/images/VS.ico");
notifyIcon.Visible = true;
Make sure you add a reference to System.Drawing.
Imaging.CreateBitmapSourceFromHBitmap
I use it like:
return Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
where source is a Bitmap, which you can get by calling your Icon's .ToBitmap() method.

WPF Image Dynamically changing Image source during runtime

I have a window with a title on it. When the user selects a choice from a drop down list, the title image can change. The problem is when the image loads, it's a blurred, stretched, and pixelated. These are PNG files I'm working with and they look good prior to setting the source dynamically.
Here's the code I'm using to change the image's source.
string strUri2 = String.Format(#"pack://application:,,,/MyAssembly;component/resources/main titles/{0}", CurrenSelection.TitleImage);
Stream iconStream2 = App.GetResourceStream(new Uri(strUri2)).Stream;
imgTitle.Source = HelperFunctions.returnImage(iconStream2);
Here are the helper functions.
public static BitmapImage returnImage(Stream iconStream)
{
Bitmap brush = new Bitmap(iconStream);
System.Drawing.Image img = brush.GetThumbnailImage(brush.Height, brush.Width, null, System.IntPtr.Zero);
var imgbrush = new BitmapImage();
imgbrush.BeginInit();
imgbrush.StreamSource = ConvertImageToMemoryStream(img);
imgbrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
imgbrush.EndInit();
var ib = new ImageBrush(imgbrush);
return imgbrush;
}
public static MemoryStream ConvertImageToMemoryStream(System.Drawing.Image img)
{
var ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
And the XAML
<Image x:Name="imgTitle" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Stretch="None" d:IsLocked="False"/>
And for Ref:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Anyone have any ideas what's up?
I can think of two things:
First, try loading the image with:
string strUri2 = String.Format(#"pack://application:,,,/MyAseemby;component/resources/main titles/{0}", CurrenSelection.TitleImage);
imgTitle.Source = new BitmapImage(new Uri(strUri2));
Maybe the problem is with WinForm's image resizing, if the image is stretched set Stretch on the image control to "Uniform" or "UnfirofmToFill".
Second option is that maybe the image is not aligned to the pixel grid, you can read about it on my blog at http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx
Hey, this one is kind of ugly but it's one line only:
imgTitle.Source = new BitmapImage(new Uri(#"pack://application:,,,/YourAssembly;component/your_image.png"));
Here is how it worked beautifully for me.
In the window resources add the image.
<Image x:Key="delImg" >
<Image.Source>
<BitmapImage UriSource="Images/delitem.gif"></BitmapImage>
</Image.Source>
</Image>
Then the code goes like this.
Image img = new Image()
img.Source = ((Image)this.Resources["delImg"]).Source;
"this" is referring to the Window object
Like for me -> working is:
string strUri2 = Directory.GetCurrentDirectory()+#"/Images/ok_progress.png";
image1.Source = new BitmapImage(new Uri(strUri2));
Me.imgAddNew.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/SPMS;component/Images/Cancel__Red-64.png", UriKind.Relative))
Try Stretch="UniformToFill" on the Image

Resources