C# WPF MenuItem with image as background programmatically - c#

I'm very bad with XAML, but I just need to create an MenuItem, that has an Image instead of Text.
I already got it working in a way, that the image is presented and loaded out of ressources, but on Mouse Over the image is disappearing.
Could you help me to get this done programmatically? In the internet I only find XAML examples but nothing programatically.
Thanks in advance.
var addOnFrameworkMenuItem = new MenuItem();
var image = new Image();
Assembly asm = Assembly.GetExecutingAssembly();
System.IO.Stream imageStream = asm.GetManifestResourceStream("MyProject.Ressources.image.png");
BitmapFrame bmp = BitmapFrame.Create(imageStream);
image.Source = bmp;
ImageBrush ib = new ImageBrush();
ib.ImageSource = bmp;
addOnFrameworkMenuItem.Width = 150;
addOnFrameworkMenuItem.Height = 31;
addOnFrameworkMenuItem.Background = ib;

Thank you Jim,
your hint helped me. For my problem on disappearing image i just needed to put the image into the header:
addOnFrameworkMenuItem.Header = image;
Additionally I found out to set most of the style properties on following way:
// Create a style to apply to the button
Style s = new Style();
s.TargetType = typeof(System.Windows.Controls.MenuItem);
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontSizeProperty, 11.0));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.BackgroundProperty, Brushes.Orange));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.ForegroundProperty, Brushes.Black));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontFamilyProperty, new FontFamily("Arial")));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontWeightProperty,FontWeights.Bold));
Trigger t2 = new Trigger();
t2.Property = System.Windows.Controls.MenuItem.IsMouseOverProperty;
t2.Value = true;
t2.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Violet));
s.Triggers.Add(t2);
addOnFrameworkMenuItem.Style = s;

Related

Fast way to check is image exists in assets in Windows Phone

My application for Windows Phone 8.1.
I need to find a way to check image aviability in assets resources in my application.
At first, I had following solution:
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var folder = await package.GetFolderAsync("Assets\\Makes");
var files = await folder.GetFilesAsync();
var result = files.FirstOrDefault(p => p.Name == imageName);
if (result != null)
{
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
Image img = new Image();
BitmapImage bi = new BitmapImage(imageUri);
img.Source = bi;
btn.Content = img;
}
else
{
TextBlock label = new TextBlock();
label.Text = text;
btn.Content = label;
}
It works. But, unfortunately, very very slow.
Anyway, next part of code working even in case if asset is not existing:
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
BitmapImage bi = new BitmapImage(imageUri);
In case if file not existing, the image is empty, but not null.
Is there are any good way, to check, if image created empty from resource?
Or a really fast way to check existing of packaged resource file?
Thank you

How to display a different text for each image?

I have a problem about the foreach instruction and a description for each image.
Right now, when the user selects an image from the list, the next message occurs (see image1)
What I want is to change this text with a different one for each image.
I've also created a class called WineModel and its code is posted below.
What should I do to display a different description for each image?
One of my ideas would be to create a vector like in this link and to display a different message for each image from the position of the vector.
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
WineModel wineModel = new WineModel();
wineModel.Image = new Uri(file, UriKind.Relative);
wineModel.Description = file + "Ana are mere" +
Environment.NewLine + "text text text text text text text text text text text";
wineModel.Price = new Random().NextDouble();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = wineModel.Image;
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = wineModel
};
this.wrapPanel.Children.Add(button);
}
http://i58.tinypic.com/nbvhu8.png
http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx

How to set MS Chart LegendItem image size

I use Image from Resources to LegendItem in WinForms
var ImageName = "ImageName";
myChart.Images.Add(new NamedImage(ImageName, Resources.Image));
LegendItem legendItem = new LegendItem();
legendItem.Name = "legend text";
legendItem.Image = ImageName;
myChart.Legends[Legend.Name].CustomItems.Add(legendItem);
But the size of Image is too small.
How can I change it?
You should use custom LegendCell in this case. This means you define the cells for your LegendItem specifying their properties. Something like this:
LegendItem legendItem = new LegendItem();
LegendCell cell1 = new LegendCell();
cell1.Name = "cell1";
cell1.Text = "legend text";
// here you can specify alignment, color, ..., too
LegendCell cell2 = new LegendCell();
cell2.Name = "cell2";
cell2.CellType = System.Windows.Forms.DataVisualization.Charting.LegendCellType.Image;
cell2.Image = "path of your img";
cell2.Size = new Size(.....);
legendItem.Cells.Add(cell1);
legendItem.Cells.Add(cell2);

Creating a button on an existing pdf using itext sharp

I have been trying for long but no success i have an existing pdf that i wan to load to my current C# app and want to create a simpe pusbutton to it , plaese cite some working code the default directory of the pdf is "C:\abc.pdf".
I am using itextsharp, C# VS 2010
Thanks
The closest solution I can find is something like the following.
static void AddPushbuttonField(string inputFile, iTextSharp.text.Rectangle buttonPosition, string buttonName, string outputFile)
{
using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
PushbuttonField buttonField = new PushbuttonField(stamper.Writer, buttonPosition, buttonName);
stamper.AddAnnotation(buttonField.Field, 1);
stamper.Close();
}
}
This came from here, but was not ranked up as a solution. The code looks good and based on my experience with itextsharp I think this will do the trick.
Source:
Adding button in a pdf file using iTextSharp
Rectangle _rect;
_rect = new Rectangle(50, 100, 100, 100);
PushbuttonField button = new PushbuttonField(writer, _rect, "button");
PdfAnnotation widget = button.Field;
button.BackgroundColor = new GrayColor(0.75f);
button.BorderColor = GrayColor.GRAYBLACK;
button.BorderWidth = 1;
button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
button.TextColor = GrayColor.GRAYBLACK;
button.FontSize = 11;
button.Text = "Text";
button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
button.ProportionalIcon = true;
button.IconHorizontalAdjustment = 0;

C# Clear a BitmapImage or the instance of it

I would like to clear a bitmap image. I've tried both
uploadImage.Source = null;
and
uploadImage.Source = "";
This is the code I used to make the image:
// BitmapImage.UriSource must be in a BeginInit/EndInit block
BitmapImage myBitmapImage = new BitmapImage();
string curItem = destinationFolder + "\\" + listBox1.SelectedItem.ToString();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(#curItem);
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
uploadImage.Source = myBitmapImage;
uploadImage.Source = null should do it. How are you checking the value of the Source property? I'd suggest Mole.
If you are relying on what you see on screen this may be wrong due to property changes not being notified. Try creating a DependencyProperty for the Image or a regular property raising a notification through the INotifyPropertyChanged interface.

Categories