Picturebox, SizeMode Zoom and padding - c#

It is possible to set SizeMode Zoom and apply padding?
The following will work:
ibPic2DLeft.SizeMode = PictureBoxSizeMode.Normal;
ibPic2DLeft.Padding = new Padding(100, 100, 50, 50);
The following will not work:
ibPic2DLeft.SizeMode = PictureBoxSizeMode.Zoom;
ibPic2DLeft.Padding = new Padding(100, 100, 50, 50);
What are the alternatives of padding that are compatible with zoom?

Put the PictureBox inside a Panel.
Set the PictureBox to Dock=Fill.
Set PictureBox SizeMode=Zoom.
Apply Padding to the Panel.

you can better go for this approach, when your image size is less than the size of the picture box, you can use normal mode and when your picture size is bigger than picture box you can use zoom mode. This is the best dynamic approach i found for my application solution in past.
Image oImg = yourImage;
if ((oImg.Height > ibPic2DLeft.Height | oImg.Width > ibPic2DLeft.Width)) {
ibPic2DLeft.SizeMode = PictureBoxSizeMode.Normal;
} else {
ibPic2DLeft.SizeMode = PictureBoxSizeMode.Zoom;
}

Related

Change bg image relative to screen c# winforms

Here's what I have:
var rand = new Random();
var files = Directory.GetFiles("C:/Projects/MOMENTUM/MOMENTUM/pics/", "*.jpg");
Image bgimage = new Bitmap(files[rand.Next(files.Length)]);
BackgroundImage = bgimage;
Rectangle UsedScreen = Screen.FromControl(this).Bounds;
if (UsedScreen.Height / UsedScreen.Width > bgimage.Height / bgimage.Width)
{
//SET IMAGE HEIGHT TO SCREEN HEIGHT
}
else
{
//SET IMAGE WIDTH TO SCREEN WIDTH
}
As you see, I first choose a random image from a specific folder and then set this as background image.
I want this application to run in full screen. However, if i set the bgimage ImageLayout property to Zoom, there will be this ugly borders and if I set it to stretch, it will look awful.
I want to achieve the following:
I get the current used screensize via screen bounds, and then adjust the image to fit the screen without being distorted.
Part of the image will be cut away but the main aim is, that the entire screen is always filled out by the image (See the comments in if). I don't know how to do this because if I try
bgimage.Height = UsedScreen.Height
I cant overwrite the image height.
Any ideas?

Resize an image in a PictureBox until it is the size of the original image

I want an image within a PictureBox that will adjust automatically depending on the size of the window but, not larger than the original image. While PictureBoxSizeMode.Zoom almost does this, when the PictureBox is larger than the image it causes the image to pixelate. I'm currently executing this code on resize:
if (pBox.Height * pBox.Height < pBox.Image.Width * pBox.Image.Height) pBox.SizeMode = PictureBoxSizeMode.Zoom;
else pBox.SizeMode = PictureBoxSizeMode.CenterImage;
While this works most of the time, occasionally the image won't revert back to zoom:
Any better way to go about this?
How about setting the maximum size to the image size:
pictureBox1.MaximumSize = pictureBox1.Image.Size;
In Combination with :
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
You should use "if (pBox.Width * pBox.Height < pBox.Image.Width * pBox.Image.Height)" if() Statement. Both dimensions of pBox is taken as height, might cause some problem. Check it

GroupBox.AutoSize and GroupBox.Text wrapping

I have two problems with the GroupBox, they appears after setting GroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink and GroupBox.AutoSize = true.
GroupBox.Text width is not taken into account at all. Sizing will occurs to fit content only and then text will get wrapped if it doesn't fit. If it cannot fit - it is simply not displayed.
There is unnecessarily big gap between bottom of the GroupBox and Label inside.
Questions:
How to make GroupBox respecting its Text property when autosizing? And how to remove that gap?
For some reasons my previous question gets on hold. Should I delete it or what?
P.S.: if you are putting on hold or something, please comment what is exactly not-clear in what I am asking!
/*
Calculate the Text Width in pixels, then set the size for the GroupBox.
*/
groupBoxA.SuspendLayout();
SizeF stringSizeLabel;
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
Font stringFont = new Font("Microsoft Sans Serif", 8.25F);
stringSizeLabel = graphics.MeasureString("SAMPLE TEXT", stringFont);
}
int iWidth = (int)(stringSizeLabel.Width * 1.35f); // Give a little extra width
int iHeight = 78; // This is a sample value
groupBoxA.Size = new System.Drawing.Size(iWidth, iHeight);
groupBoxA.MinimumSize = new System.Drawing.Size(iWidth, iHeight);
groupBoxA.ResumeLayout(false);
groupBoxA.PerformLayout();

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".

How do I get or set the position of an image on a Silverlight Canvas control

I have an Image control as the child of a Canvas control. If I load the Image control with a bitmap and set the Stretch property to Uniform, I get a centered image. However, I can find no way to either set or retrieve the position of the Image. Solution anyone?
Here is some code to make understanding the problem a bit easier:
Image img = new Image();
img.Height = maxSize.Height;
img.Width = maxSize.Width;
img.Stretch = Stretch.Uniform;
img.Source = bmp;
myCanvas.Children.Add(img);
// returns 0:
MessageBox.Show(((Image)myCanvas.Children[0]).GetValue(Canvas.LeftProperty).ToString());
MessageBox.Show(((Image)myCanvas.Children[0]).GetValue(Canvas.TopProperty).ToString());
// in order to set explicitly set the values I have to know what they are?
Double left = {the current left value};
Double top = {the current top value};
((Image)myCanvas.Children[0]).SetValue(Canvas.LeftProperty, left);
((Image)myCanvas.Children[0]).SetValue(Canvas.TopProperty, top);`
I select the code and press the {} button, but it does not work for me, neither does entering code ticks manually.
Use the Canvas.LeftProperty and Canvas.TopProperty
var left = (double)image.GetValue(Canvas.LeftProperty)
left += 50;
image.SetValue(Canvas.LeftProperty, left);

Categories

Resources