get the highest qulity image/screenshot C# wpf - c#

I'm writing a little program that tranfers a screen shot of a user screen to my wpf image control but when the image is displayed it is not full,not the whole screen is displayed even if the action was done through my computer and not my leptop and it is blurry
Bitmap bmp=
new Bitmap((int)SystemParameters.PrimaryScreenWidth(int)SystemParameters.PrimaryScreenHeight);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
the code above is the code to take the screenshot
BitmapImage bmI = new BitmapImage();
bmI.BeginInit();
bmI.UriSource = new Uri(FullPath);
bmI.EndInit();
Screen_Shot.Source = bmI;
And this code is the code to dislpay the image, Screen_Shot is the image name, and the full path is where i put the bitmap image
I tried to use
Screen_Shot.Stretch = Stretch.Fill; and UseLayoutRounding="True" SnapsToDevicePixels="True"
but none of them seems to get the job's done
this is an exsample of a screenshot i took from my own pc

You want to set the Stretch property to Uniform or else your image will be distorted as it will fill regardless of the Image control's size/shape. You also have to remember that the screen size may be different than your programs window size. If you don't uniformly stretch the image it will distort almost always (even if it's just a bit).
Regarding your "blurry" image, you need to specify the rendering option of you Screen_Shot image control or it will set the image to a lower quality that it can render more easily. This is honestly better to just set in the xaml. I normally use Fant, but look other options specified here.
<Image Name="Screen_Shot" RenderOption.BitmapScalingMode="Fant"/>

Related

C# Zoomed image in Picturebox gets cropped in top left

I want to make very simplistic paint/image editor. Mainly, for pixel editing, but that doesn't seem relevant.
To ease up my effort, I decided to keep the image size at 16x16.
I populate the form, add a PixelBox and slap a default image on it.
Of course, I need to make the pixels visible, set the interpolation to NearestNeighbor.
Then, I stretch the pixelbox to 320x320. And there the situation arises.
The image is displayed as thus:
Cropped image
Could someone shed some light on this? This is just a 16x16 image with a checkerboard pattern that I made, but I can't figure out why it is displayed with that offset at the top left.
Also, no code as been yet added. I assume this is default behavior?
If you look at the examples on the page that exact same error happens, so it must be a bug on the PixelBox.
Instead of using a custom control for this type of operation just use the standard PictureBox and scale the image by yourself:
public Bitmap ScaleBitmap(Bitmap src, Size NewSize)
{
Bitmap bmp = new Bitmap(NewSize.Width, NewSize.Height, src.PixelFormat);
Graphics g = Graphics.FromImage(src);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(src, new Rectangle(Point.Empty, NewSize), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}

C# Layering transparent images

I'm fairly new to working with c#, so I am sure there is a way to accompish this, but I have been unable to find an answer that works.
I am making a simple game where you create a pizza (similar to dominoes interactive ordering system). The user selects the toppings from a list and they appear on the pizza image. I planned to simply change the visibility of the topping .pngs when the items are selected, however, the last .png to appear covers up all previous ones.
I have tried using picture boxes and panels.
When using picture boxes, only the top visible image shows. I have the .pngs backgrounds set to transparent, and while they do show the form's background color, they mask the other .pngs.
When I used the panels, I had problems with the upper images parenting with the lower ones, so if I changed the visibility of the bottom one, all ones above it were hid as well.
I appreciate any help and advice.
Mix images at runtime and add it to your picturebox:
Bitmap bmp = (Bitmap)Bitmap.FromFile("pizza.png"); //or get it from any other source
Bitmap bmp2 = (Bitmap)Bitmap.FromFile("over.png"); //or get it from any other source
Bitmap bmpdest = new Bitmap(bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(bmpdest);
g.DrawImage(bmp, new Point(0, 0));
g.DrawImage(bmp2, new Point(0, 0));
PictureBox1.Image = (Image)bmpdest;

Is it possible to change the quality of the image interpolation in WPF Image control?

Image control - placed into Viewbox.
For automatic scaling when the window size changes.
Everything works perfect.
Is it possible to change the type of interpolation in my case?
For example select Bicubic or Bilinear.
Or for automatic scaling of such a choice is not available?
BitmapImage bmp=new BitmapImage(new Uri("c:/temp/1.jpg"));
ImageSource pic = bmp;
Viewbox vb = new Viewbox();
vb.Stretch = Stretch.UniformToFill;
vb.StretchDirection = StretchDirection.DownOnly;
Image img=new Image();
img.Source = pic;
vb.Child = img;
As far as I know you can't specify bicubic or bilinear interpolation for a bitmap in WPF, (the default is linear) but you can set RenderOptions.BitmapScalingMode option to get better control of the display quality of the scaled bitmap. For example:
RenderOptions.BitmapScalingMode="NearestNeighbor" or RenderOptions.BitmapScalingMode="HighQuality"
There's more info on each of the scaling modes on MSDN http://msdn.microsoft.com/en-us/library/system.windows.media.bitmapscalingmode
If you still have issues with blurry graphics try setting UseLayoutRounding="True" in your root element. This will disable sub-pixel positioning of elements that can can cause jagged lines in WPF applications

How to search for an image on screen in C#?

I want to search for an image on screen using C# or other .NET languages(like powershell). Something like i give an image location in the file system and the code consider the whole screen as an image and search the image in the file system in the big image(the screen) then returns the image position on screen. I can't find this kind of things in the .net classes.
Thanks.
This is a pretty specific problem, which is why you won't find it in the .NET Framework. You should break down your problem in smaller pieces:
Load image from file on disk
Use System.Drawing.Image.FromFile().
Acquire an image of the screen, i.e. a screen shot
Use System.Drawing.Graphics.CopyFromScreen():
Bitmap CaptureScreen()
{
var image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
var gfx = Graphics.FromImage(image);
gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
return image;
}
Find image inside image
See answer to this question.
i have made a program which used a lib i coded in c# which will return the value of x and y of a small image inside a bigger image (screenshot) you can see all the details here https://www.nulled.io/topic/22223-an-advanced-image-search-library-saeedsearchdll/

Make overlapping picturebox transparent in C#.net

I have two overlapping pictureboxes.The images of both picture boxes have some transparent pixels.I want to see the bottom picture box through the transparent pixels of the overlapping picture box.
I tried setting the background color of both picture boxes as transparent.But it just sets the back color of the picture box to the background color of the form.
Clearly you are using Winforms. Yes, transparency is simulated by drawing the pixels of the Parent. Which is the form, you only see the form pixels, stacking effects don't work. There's a KB article that shows a workaround for this. It is painful. Another approach is to not use PictureBox controls but just draw the images in the form's Paint event.
Consider WPF, it has a very different rendering model that easily supports transparency.
Solutions to that problem might be various, and it mainly depends on your skills and amount of work will depend on kind of images you're dealing with. For example if images are always same resolution, size and overlapping image supports transparency you could try to do manipulation of two Image objects and draw one over another, then display it in PictureBox. Or if you will need to do it multiple times in various places of your app you could even consider creating your own UserContriol.
Code in answer of this question, method ResizeImagein particular, show how to create resized, good quality image, all you need it is to change it a little. Make it to get two Images as input parameters, and change it to draw one image over another.
Changes might look like this
public static Bitmap CombineAndResizeTwoImages(Image image1, Image image2, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the images into the target bitmap
graphics.DrawImage(image1, 0, 0, result.Width, result.Height);
graphics.DrawImage(image2, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
And use it, for example, like this:
pictureBox1.Image = CombineAndResizeTwoImages(Image.FromFile("c:\\a.png"), Image.FromFile("c:\\b.png"), 100,100);
But that its only example, and you must tune it up to your needs.
Good luck.
If it's one PictureBox inside another, you can use:
innerPictureBox.SendToBack();
innerPictureBox.Parent = outerPictureBox;

Categories