Label Image mode to stretch - c#

I wrote this code to add my Labels:
JArray a = JArray.Parse(temp);
Label[] labels = new Label[100];
foreach (JObject o in a.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
string name = p.Name;
string value = p.Value.ToString();
if (name == "name")
{
labels[counter] = new Label();
//Image i = Image.FromFile("item.jpg");
labels[counter].Text = value;
labels[counter].Image =Image.FromFile("item.jpg");
//labels[counter].Image
//labels[counter].BackColor = Color.Blue;
labels[counter].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
labels[counter].Top = height;
height += 50;
Controls.Add(labels[counter]);
}
}
}
The Image should stretch to the Label Size. How can I do this?

The abilities to show and manipulate images and text are spread out in a rather wild fashion among Winforms controls.
A Label can not stretch its Image.
A PictureBox and a Panel can but they don't show their Text
A Button can do both but will always be a Button, no matter how you style it..
So to get a combination you will need to either owner-draw something:
DrawImage in an overload to get the right size of the image, then add Image to Label
Or DrawString the Text onto a Panel to show it alongside the Image
or you could combine two controls with the right abilities:
You can create a Panel and set its BackgroundImage to your Image and its BackgroundImageLayout=Stretch. Then you can add your Label with its Text set to the Panel's controls collection:
// preparation for testing:
Image image = Image.FromFile("D:\\stop32.png");
Size size = new Size(77, 77);
// create the combined control
// I assume your Label is already there
Panel pan = new Panel();
pan.Size = size;
// or, since the Label has the right size:
pan.Size = label.Size; // use Clientsize, if borders are involved!
pan.BackgroundImage = image;
pan.BackgroundImageLayout = ImageLayout.Stretch;
label.Parent = pan; // add the Label to the Panel
label.Location = Point.Empty;
label.Text = "TEXT";
label.BackColor = Color.Transparent;
// add to (e.g.) the form
pan.Parent = this;
Set Borders as you like..
One more option: If all Images should have the same Size and if it is 256x256 pixels or less you could add them to an ImageList. This will stretch them to the ImageList.ImageSize in a very simple way and you can add them to your Label..

Very simple:
VB
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
C#
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);

If you are using WinForms you try try below:
labels[counter].Size =
new Size(labels[counter].Image.Width, labels[counter].Image.Height);

This works perfect for me:
Just set the image in design mode (don't use imagelist) use the "Image" option
if we have label1 as the label where we want the image, put the next line inside the constructor:
label1.Image = new Bitmap(label1.Image, label1.Size);
I was trying the solution of Zibri, but deform the image in my case.

Related

WinForms Checkbox button image alignment

I'm trying to make a toggle button inside C# WinForms application. Now I have managed to make the toggle button look and feel with the suggestion from my previous post.
Now the problem is, I'm not able to centre align the images from my ImageList properly on the button, so its showing some of the back colors on its edges. Please see the below images for clear view.
How can I get rid of this white edges?
Things tried so far:
FlatAppearance of the Button is set to Flat.
Tried with Transparent background color but that didn't work.
ImageAlign is set to MiddleCenter.
Code generated by WinForms designer
//
// checkBox1
//
this.checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
this.checkBox1.BackColor = System.Drawing.Color.White;
this.checkBox1.CausesValidation = false;
this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.BottomLeft;
this.checkBox1.Cursor = System.Windows.Forms.Cursors.Hand;
this.checkBox1.FlatAppearance.BorderSize = 0;
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.ForeColor = System.Drawing.Color.White;
this.checkBox1.ImageIndex = 0;
this.checkBox1.ImageList = this.imageList1;
this.checkBox1.Location = new System.Drawing.Point(88, 178);
this.checkBox1.Margin = new System.Windows.Forms.Padding(0);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(98, 62);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Sample Button";
this.checkBox1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged_1);
Update: I'v managed to get rid of these white edges by reducing button size 1px (both x, y) from the image size. Image size is: 99x63, Button size 98x62. But I'm not sure whether this is the correct way to do it.
This is simple. Choose therese settings:
checkBox1.FlatStyle = FlatStyle.Flat;
checkBox1.FlatAppearance.BorderSize = 0;
// make all four (!) BackColors transparent!
checkBox1.BackColor = System.Drawing.Color.Transparent;
checkBox1.FlatAppearance.CheckedBackColor = Color.Transparent;
checkBox1.FlatAppearance.MouseDownBackColor = Color.Transparent;
Note that with FlatStyle.Flat the checkboxbutton reserves 8 horizontal pixels, 6 at the left and 2 at the right edge and will cut off 8 pixels from your Image unless you enlarge it like so:
checkBox1.Size = new Size(imageList1.ImageSize.Width + 8, imageList1.ImageSize.Height);
Now all pixels are displayed, however the control is not visibly left aligned until you move it to the left by 6 pixels!
Looking at your example both issues are probably not important, though, but sometimes they are..

c# dynamically created label size not wide enough

When dynamically creating a label a part of it's text is missing. This is because the size of the label is not wide enough, it cuts of a part of the the actual string.
How do i make that simply stop? I dont want to nor do i remember setting a size to the label. Should it not just continue untill the string is empty?
Example: value = "Berserker's Iron Axe", it only displays "Berserker's Iron", because i have no size set it cust off a part of the string.
PictureBox finalResult_pBox = new PictureBox {
Name = "finalResult_pBox" + i,
Size = new Size(64, 64),
Padding = new Padding(0),
BorderStyle = BorderStyle.FixedSingle,
ImageLocation = spidyApi_idByName_result.results[i].img.ToString(),
};
MessageBox.Show(spidyApi_idByName_result.results[i].name.ToString());
Label finalResult_itemName_label = new Label{
Name = "finalResult_itemName_label" + i,
Text = spidyApi_idByName_result.results[i].name,
};
FlowLayoutPanel finalResult_panel = new FlowLayoutPanel{
FlowDirection = FlowDirection.TopDown,
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
Name = "result_flowLayoutPanel" + i,
Size = new System.Drawing.Size(790, 64),
TabIndex = i,
};
finalResult_panel.Controls.Add(finalResult_pBox);
finalResult_panel.Controls.Add(finalResult_itemName_label);
result_flowLayoutPanel.Controls.Add(finalResult_panel);
Why is this happening and how do i fix this?
As long as you keep Label.AutoSize = true, which is the default it should size itself automatically.
You may want to do some tests to see the actual dimensions, like
setting a BorderStyle
or a BackColor
or inserting a space in the Text, so it gets a chance to go to a second line..
Of course you could measure the Text (with e.g. TextRenderer.MeasureText(text, Label.Font); but that will certainly not be necessary for the text simply to show.. It is useful for more demanding layout control, though.
So the first thing is to make sure that AutoSize is either true or that you measure and set the Size of the Label..

Printing bitmap from Silverlight - image blurry

I'm trying to print image from Silverlight application. I have pretty good quality scans (TIFF) with resolution 1696x2200
When I print - I get PrintableArea from PrintDocument and it's 816x1056
What I do - I resize bitmap to Printable area (to fit document to page) and result I get is blurry image. I understand this is scaling problem (most likely), but how do I scale properly so it looks good? When I display document inside Image and just set image size - it looks good.
For resizing I'm using WriteableBitmapEx extensions and tried both types of resize (Nearest neighbor and bilinear)
Code:
var printDocument = new PrintDocument();
printDocument.PrintPage += (s, ea) =>
{
var printableArea = ea.PrintableArea;
var bitmap = this.currentPreviewPage.FullBitmap.Resize((int)printableArea.Width, (int)printableArea.Height, WriteableBitmapExtensions.Interpolation.Bilinear);
var image = new Image { Source = bitmap };
var canvas = new Canvas { Width = bitmap.PixelWidth, Height = bitmap.PixelHeight };
canvas.Children.Add(image);
ea.PageVisual = canvas;
ea.HasMorePages = false;
};
printDocument.PrintBitmap("Silverlight Bitmap Print");
How document looks on screen (inside Image)
And this is printed:
Rather than using the WriteableBitmapEx extensions, when declaring your Image element, try setting the Stretch property so that it stretches based on your maximum specified dimensions:
var image = new Image { Source = bitmap, Stretch = Stretch.UniformToFill };
Blilinear filter tends to blur images.You may want to try WriteableBitmapExtensions.Interpolation.NearestNeighbor instead to see if you get better results
In my case it was enough to set UseLayoutRounding="True".

Programmatically assigning Margin and/or Padding to a Label

In trying to get some labels in a TableLayoutPanel to move from the top left of their cells to the center of the cells, I'm trying to experiment with adding padding and/or margins.
However, nothing I've tried works. Here's the code I've tried and the results:
// Setting the padding just cuts off the bottom part of the text
//lbl.Padding = new System.Windows.Forms.Padding(1);
// How to set Margin?
//lbl.Margin = new System.Windows.Forms.Margin(1); <- This mimics "Padding" but is not recognized
//lbl.Margin = new Thickness(6); <- This is the only example I could find, but it's for WPF
Try:
lbl.Margin = new Padding(1);
You might also want to do:
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.AutoSize = false;
labelName.Style.Add("Margin", "10px");

Adding transparent Gif images to list view in c#

I am trying to add a transparent Gif image to list view, I used the following code snippet
to add gif image.
// control as ListView
void AddGiftoListView(Control parent)
{
Bitmap img = (Bitmap)Properties.Resources.madLoader; // Transparent gif image
Size sz = img.Size;
PictureBox pbGif = new PictureBox();
pbGif.Size = img.Size;
pbGif.Image = img;
parent.Controls.Add(pbGif);
Point p = new Point(1, 1);
pbGif.Location = p;
pbGif.Show();
}
But the gif is not transparent, when the list view filled with text, we can see gif on top of that with white background. is there any way ro resolve this issue?
Thanks,
Shiju P K
Try making your form's TransparencyKey as white
this.TransparencyKey = Color.White;

Categories

Resources