c# Unable to set the size of the ToolStripStatusLabel? - c#

In the toolstripstatuslabel, I'm adding images and displaying it as shown below.
Case 1: Setting the image list size [Not working]
Case 2: Setting the size of the label.[Not working]
In both the above cases, size is not getting reflected.
StatusStrip statusStrip = new StatusStrip();
//Set the size of the status bar
statusStrip.AutoSize = false;
ImageList imgList = new ImageList();
...Add resource images to image list
imgList.ImageSize = new System.Drawing.Size(50, 50);
//Set the images on the status strip
ToolStripStatusLabel add = new ToolStripStatusLabel();
add.Image = jobImgList.Images[0];
//Set auto size to false, so specify the size
add.AutoSize = false;
add.Size = new System.Drawing.Size(50, 50);
statusStrip.Items.Add(add);
Is there any other property that needs to be changed other than setting AutoSize to false!?

The solution is as simple as that!
statusStrip.ImageScalingSize = new Size(40, 40);
Reference:
How to increase the size of the buttons on a tool strip?

Related

How to overlap WPF Images

Is there a way overlap say three images to have them overlap. I am have a case where I bring the top element to the top but it is showing the base layer underneath instead of the second.
Here is what I mean:
Should be this:
The code for it:
// Bottom Box
this.BottomBox.BackColor = Color.Transparent;
this.BottomBox.BackgroundImage = Resource.BottomBox;
this.BottomBox.Name = "BottomBox";
// Middle Box
this.Middle.BackColor = Color.Transparent;
this.Middle.BackgroundImage = Resource.MiddleBox;
this.Middle.Parent = BottomBox;
this.Middle.Name = "Middle";
// Top Box
this.TopBox.BackkColor = Color.Transparent;
this.TopBox.BackgroundImage = Resource.TopBox;
this.TopBox.Parent = MiddleBox;
this.TopBox.Name = "TopBox";
The easiest way to overlap controls/views (images, etc) in WPF is to put them inside a Canvas or a Grid...
In a Canvas, you can alter there relative positions using the attached properties Canvas.Top and Canvas.Left.
In a Grid, you can alter their relative positions using margins...
You can do this in the Constructor of the views immediately below the InitializeComponent method.
Sample Code:
// Bottom Box
this.BottomBox.BackColor = Color.Transparent;
this.BottomBox.BackgroundImage = Resource.BottomBox;
this.BottomBox.Name = "BottomBox";
// Middle Box
this.Middle.BackColor = Color.Transparent;
this.Middle.BackgroundImage = Resource.MiddleBox;
this.Middle.Parent = BottomBox;
this.Middle.Name = "Middle";
// Top Box
this.TopBox.BackkColor = Color.Transparent;
this.TopBox.BackgroundImage = Resource.TopBox;
this.TopBox.Parent = MiddleBox;
this.TopBox.Name = "TopBox";
var canvas = new Canvas();
canvas.Children.Add(this.BottomBox);
canvas.Children.Add(this.TopBox);
canvas.Children.Add(this.Middle);
Canvas.SetLeft(this.BottomBox, 50);
Canvas.SetTop(this.BottomBox, 100);
Canvas.SetLeft(this.Middle, 50);
Canvas.SetTop(this.Middle, 50);
Canvas.SetLeft(this.TopBox, 50);
Canvas.SetTop(this.TopBox, 0);
//put canvas as the main element to display in the view
Try it out and leave a comment if you have any further issue.

PictureBox not sizing properly

I created a PictureBox and load an image into it, and I wanted the pictures to have a maximum size (let's say 250px). Here is the code I'm using at that moment
PictureBox cellPictureBox = new PictureBox();
cellPictureBox.AutoSize = false;
cellPictureBox.Dock = DockStyle.Fill;
cellPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
cellPictureBox.Image = Base64ToImage(data.ToString().Trim());
cellPictureBox.Width = 250;
cellPictureBox.Height = 250;
When I load the first image, it's quite large. When I load a second image, it's automatically scaled down to be very tiny, and the newly loaded image takesthe large size the first image had. This trend continues as I add more rows to my data.
What can I do to help manage the sizing of my images? Making a custom control I've been told is useful, but I don't see what is happening that I have wrong.
To set maximum and minimum sizes, have a look at the following:
PictureBox cellPictureBox = new PictureBox();
cellPictureBox.AutoSize = false;
cellPictureBox.Dock = DockStyle.Fill;
cellPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
cellPictureBox.Image = Base64ToImage(data.ToString().Trim());
cellPictureBox.MinimumSize = new Size(100, 100); // or whatever size you want.
cellPictureBox.MaximumSize = new Size(250, 250);
If you only want to set a limit on one dimension, for example, the width, but the height is allowed to be anything, then use int.MaxValue:
cellPictureBox.MaximumSize = new Size(250, int.MaxValue);

Print FixedDocument onto label without PrintDialog (setting page margin and size myself)

tl;dr: Why do I need to set an extra "magic" offset on the left side when printing with WPF, XpsDocumentWriter and FixedDocument compared to printing with WinForms and PrintDocument?
I am building a WPF app that creates a FixedDocument and prints it to a label printer via XpsDocumentWriter. It's split in two parts, as I want the printer part to be able to receive and print a FixedDocument over the network from another app.
It's for use with a point of sale system, so I do not want to show a PrintDialog for every print. Size of labels and the printer name is stored in a template file. The things to be printed is in the template file combined with some data. (In below example printer name is taken from a combobox for now and just some demo things to be printed).
I retrieve the printer queue with the given printer name, and I can print to the printer, but the margins are quite off - the print is too far to the left, outside of the label and by more than just the liner margin (distance from label edge to the roll edge). By trial'n'error I found the magic offset to be around 20 plus the liner margin, but I guess that's different on another printer.
The old version of this app (which is made with WinForms and PrintDocument) prints allright (no matter how the printer properties' paper is setup). The wish from business is that all setup is in the template file, completely disregarding printer properties setup. That is how it is functioning now in the WinForms app.
What am I forgetting to set, what's the gotchas?
Should I do it some other way?
Do I need to set the PageMediaSize and PageMediaType, and why?
I'd like to do checks on PrinterCapabilities, but not if they depend on the current printer properties like the page size set in the properties. That one I'd like to set in code.
Update 11/3
I now tried printing to both a Metapace and a Zebra labelprinter, and both is happy with the 20 unit offset on the left side. But... why is it needed? When printing to pdf/xps, I get the offset to the left plus the margin on both sides. This I do not get with the old program in Winforms?
And do I need to do Measure, Arrange and UpdateLayout?
Part one - the FixedDocument creator
var printerName = CmbPrinters.SelectedValue.ToString();
var size =
(FlexTemplate.TemplateParser.TemplateFunctions.Size)
template.Functions.First(f => f is FlexTemplate.TemplateParser.TemplateFunctions.Size);
var labelSize = new Size(size.Width.Value, size.Height.Value);
// Dimensions are 1/96th of an inch, socalled Device Independent Pixels (matches 96 DPI, which is normal?)
var linerMargin = 4.7244094488189; // 1.25 mm
var magicOffset = 20; // 5.29 mm
// The document
var doc = new FixedDocument();
doc.PrintTicket = new PrintTicket();
var printTicket = (PrintTicket) doc.PrintTicket;
printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.Unknown, size.Width.Value, size.Height.Value);
// New page
var page = new FixedPage
{
Width = labelSize.Width + magicOffset + 2 * linerMargin,
Height = labelSize.Height,
Margin = new Thickness(magicOffset + linerMargin, 0, linerMargin, 0)
};
// Page content
var border = new Border
{
Width = labelSize.Width,
Height = labelSize.Height,
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(4)
};
FixedPage.SetLeft(border, 0);
FixedPage.SetTop(border, 0);
page.Children.Add(border);
// Next item on page
var tb = new TextBlock
{
Text = "Hello World!",
FontFamily = new FontFamily("Algerian Regular"),
FontSize = 30,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
var content = new Border
{
Child = tb,
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};
FixedPage.SetLeft(content, 10);
FixedPage.SetTop(content, 10);
page.Children.Add(content);
// Next item on page
var tb2 = new TextBlock
{
Text = "SomethingElse",
FontFamily = new FontFamily("Segoe UI"),
FontSize = 20,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
var content2 = new Border
{
Child = tb2,
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};
FixedPage.SetLeft(content2, 20);
FixedPage.SetTop(content2, 55);
page.Children.Add(content2);
//// Add the page to the document
//var pc = new PageContent(); // Old way of doing it (.NET 3.5 and earlier)
//((IAddChild)pc).AddChild(page); //
//doc.Pages.Add(pc); //
var pc = new PageContent();
pc.Child = page;
doc.Pages.Add(pc);
// Send it to the printer
FlexPrinter.Print(printerName, labelSize, doc);
Part two - the FixedDocument printer
// Get XpsDocumentWriter
PrintQueue queue = new PrintServer().GetPrintQueue(printerName);
var dw = PrintQueue.CreateXpsDocumentWriter(queue);
dw.Write(document);

Button image sides cropped

I'm trying to fit image to button perfectly.
But the image is cropped on its right and bottom faces, see attached print screen:
I edited the button as follows:
var l_oStopImage = Image.FromFile(#"C:\Users\AmitL\Downloads\Button-2-stop-icon72p.png");
var l_oStopPic = new Bitmap(l_oStopImage , new Size(btnStopOperation.Width, btnStopOperation.Height));
btnStopOperation.Image = l_oStopPic ;
btnStopOperation.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
btnStopOperation.TabStop = false;
btnStopOperation.FlatStyle = FlatStyle.Flat;
btnStopOperation.FlatAppearance.BorderSize = 0;
I also tried to edit the BackgroundImageLayout but none of the ImageLayouts fixed the problem..
Any suggestions?
Thanks in advance
1https://msdn.microsoft.com/en-us/library/system.windows.forms.imagelayout(v=vs.110).aspx
You should use stretch, I suggest in designtime (this is not java where you have to add elements by code):
this.buttonOk.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.buttonOk.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonOk.BackgroundImage")));
this.buttonOk.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonOk.Location = new System.Drawing.Point(475, 15);
this.buttonOk.Name = "buttonOk";
this.buttonOk.Size = new System.Drawing.Size(50, 50);
this.buttonOk.TabIndex = 11;
this.buttonOk.UseVisualStyleBackColor = false;
this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
And it will work, done it many times before
I got this code from my own working Form1.Designer.cs but because of that: please use the Visual Studio designer and don't try to write all this code / logic in your constructor or something.
The problem is because you are showing an image with the same size as your button.
When you want an image fit in your button, the width and height of image should be at least 1 point less than your button size. (or in other word, you can set your button width and height 1 point more than the image size).
So you can change your code to this:
var l_oStopPic = new Bitmap(l_oStopImage ,
new Size(btnStopOperation.Width-1, btnStopOperation.Height-1));

How do I resize a System.Windows.Forms.ToolBar?

I've not bothered with panels, docking, or anchors. I've simply thrown together a ToolBar control (not ToolStrip) and seem unable to size it.
System.Windows.Forms.ToolBar tb = new System.Windows.Forms.ToolBar();
// Reports 292x28 (approx) if I check width and height
// Basically the width of the form and I assume a default height
tb.Size = new System.Drawing.Size(195, 48);
// Reports 48x48, but does not actually create buttons of that size
// (It reports 48x48 because I'm retrieving 48x48 icons from a ResourceManager (resx))
tb.ButtonSize = new System.Drawing.Size(48, 48); //
The closest thing I found to making my ToolBar taller was:
http://bytes.com/topic/c-sharp/answers/241614-changing-height-toolbar-button
Although it's rather dated. And I didn't understand it. ToolBarButtons don't have Height, Width, or Size properties.
I'm using SharpDevelop, coding completely by hand on Vista, with all the .NET frameworks.
EDIT:
Here is the EXACT code that I am currently using.
#region ImageList/Toolbar
ImageList toolbarImages = new ImageList();
Image wizardToolbarImage = (Bitmap) rm.GetObject("wizard");
Image optionsToolbarImage = (Bitmap) rm.GetObject("configure");
toolbarImages.Images.Add(wizardToolbarImage);
toolbarImages.Images.Add(optionsToolbarImage);
ToolBar toolbarMain = new ToolBar();
toolbarMain.Size = new Size(195, 25); // no effect
ToolBarButton wizardToolbarButton = new ToolBarButton();
ToolBarButton optionsToolbarButton = new ToolBarButton();
wizardToolbarButton.ImageIndex = 0;
wizardToolbarButton.ToolTipText = "Wizard!";
optionsToolbarButton.ImageIndex = 1;
optionsToolbarButton.ToolTipText = "Options!";
toolbarMain.Buttons.Add(wizardToolbarButton);
toolbarMain.Buttons.Add(optionsToolbarButton);
toolbarMain.Appearance = ToolBarAppearance.Normal;
toolbarMain.ButtonSize = new System.Drawing.Size(48, 48); // no effect
toolbarMain.ImageList = toolbarImages;
toolbarMain.ButtonClick += new ToolBarButtonClickEventHandler(toolbarMain_Click);
Controls.Add(toolbarMain);
#endregion
In just about every winforms application I've written, regardless of language or framework, the toolbar could only be made taller by using larger icons.
You can also put the toolstrip inside a Panel and set the Dock property of the tool strip to Fill. And then you can size the Panel to whatever size you need.

Categories