WPF Screen Resolution - wpf

I have been working on WPF desktop application and have graphics according to a particular screen resolution. I need to scale the all the margins based on screen resolution. How can I achieve this?

you can use :
System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;
make a little search :)

To get the Screen Resolutions we have to use the dpi factor value of system, use the below code as per this article (please read it in full to understand):
using System.Windows.Media;
Window mainWindow = Application.Current.MainWindow;
PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(mainWindow);
Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
double dpiWidthFactor = m.M11;
double dpiHeightFactor = m.M22;
double ScreenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor; //calculated correct value
double ScreenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor; //calculated correct value

Add reference to :- System.Windows.Forms
var width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;

Related

How to set WPF window size to fit the screen?

I want to shrink the window so it fits on a screen.
Normally my window has size of 1000x800 so there is enough space in most scenarios but I'm also checking screen area to make sure it's not bigger. However my boss gave me his pc where he has screen scale set to 150% and the window becomes bigger then available space. I thought I solved this by dividing screen size by it's current dpi but turns out this does not work in all scenarios.
This is the method I'm using to set the size based on current DPI:
private void setMainWindowDimensions() {
//set window size and position with respect to current screen
var windowInteropHelper = new WindowInteropHelper(nativeWindow);
var screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
var dpi = screen.getScale();
nativeWindow.Width = Math.Min(screen.WorkingArea.Width / dpi.x, 1000);
nativeWindow.Height = Math.Min(screen.WorkingArea.Height / dpi.y, 800);
}
And here is the getScale() extension method I'm using to get the scale factor:
//https://stackoverflow.com/questions/29438430/how-to-get-dpi-scale-for-all-screens
public static (uint x, uint y) GetDpi(this S screen, DpiType dpiType = default) {
var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
var mon = MonitorFromPoint(pnt, 2/*MONITOR_DEFAULTTONEAREST*/);
GetDpiForMonitor(mon, dpiType, out var dpiX, out var dpiY);
return (dpiX, dpiY);
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062(v=vs.85).aspx
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags);
//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY);
This is is form the How to get DPI scale for all screens? answer.
Above code worked when I changed the scale of main monitor on my work pc, it correctly returned dpi of (1.5, 1.5). Few months later and my boss is back with he's computer saying it still don't work.
I figured out the DPI is taken form main screen, even if the window is opened on the other and here I still can't figure out how to get correct values.
I was following this article But it doesn't work at all. The methods form the NativeHelpers project are returning some values but the result is the same as with original method. Basically the SetPerMonitorDPIAware fails and I don't know why. I used slightly modified version and I thought that there is something wrong with my project configuration, but even when I create new WPF project, extending the window from PerMonitorDPIWindow class, the result is the same - it throws "Enabling Per-monitor DPI Failed."
private void createWindow() {
var c = logCtx("Creating main applicaton window", ONE_TIME_LOG);
var pma = PerMonitorDPIHelper.SetPerMonitorDPIAware();
if (pma == 0) log("Could not set per monitor DPI awareness.", WARNING_LOG);
else log("Per monitor awareness was set.", SUCCESS_LOG);
window = new CWindow();
nativeWindow.AllowDrop = true;
setMainWindowDimensions();
c.close();
}
private void setMainWindowDimensions() {
//set window size and position with respect to current screen
var windowInteropHelper = new WindowInteropHelper(nativeWindow);
var screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
var dpi = screen.getScale();
var sdpi = PerMonitorDPIHelper.GetSystemDPI();
var wdpi = PerMonitorDPIHelper.GetDpiForWindow(windowInteropHelper.Handle);
log($#"DPI awareness: {PerMonitorDPIHelper.getPerMonitorDPIAware()}");
log($#"system DPI: {sdpi}, window DPI: {wdpi}, screen DPI: {dpi}");
nativeWindow.Width = Math.Min(screen.WorkingArea.Width / dpi.x, 1000);
nativeWindow.Height = Math.Min(screen.WorkingArea.Height / dpi.y, 800);
}
private void onLoaded(object sender, RoutedEventArgs e) {
var c = logCtx("Main window loaded", ONE_TIME_LOG);
setMainWindowDimensions();
c.close();
}
Here you can see returned DPI is 1.5 even if the window is opened on screen that is not scaled.
Since I have a drawing app, I'm also using the following method to determine real scale coordinates:
private (double x, double y) density {
get {
var m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget
.TransformToDevice;
return (96 / m.M11 / 25.4, 96 / m.M22 / 25.4);
}
}
This solution is from this question, and it also don't work in various dpi scenarios.
I've also read in one of the comments that shcore is available only since Windows 8. Our PCs have Win 10 but we also want to support Windows 7.
There is also on more strange thing. My boss PC is actually a laptop with only one, main screen, yet it still doesn't get proper DPI. I don't have access to it right now so don't have logs I've just added...
Is there any method which works in all scenarios? It's seem that such such thing should require 2 lines of code yet it's became so convoluted I've already wasted 2 day on this and I'm out of ideas :(

Place an Image scaled at the width available space

I created a code that works, but I'm not sure that it's the best way to place an Image scaled automatically to the available width space. I need to put some content over that image, so I have a LayeredLayout: in the first layer there is the Label created with the following code, on the second layer there is a BorderLayout that has the same size of the Image.
Is the following code fine or is it possible to do better?
Label background = new Label(" ", "NoMarginNoPadding") {
boolean onlyOneTime = false;
#Override
public void paint(Graphics g) {
int labelWidth = this.getWidth();
int labelHeight = labelWidth * bgImage.getHeight() / bgImage.getWidth();
this.setPreferredH(labelHeight);
if (!onlyOneTime) {
onlyOneTime = true;
this.getParent().revalidate();
}
super.paint(g);
}
};
background.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
background.getAllStyles().setBgImage(bgImage);
Shorter code:
ScaleImageLabel sl = new ScaleImageLabel(bgImage);
sl.setUIID("Container");
You shouldn't override paint to set the preferred size. You should have overriden calcPreferredSize(). For ScaleImageLabel it's already set to the natural size of the image which should be pretty big.

Icons sizing not increasing in higher DPI

I am facing this issue while DPI is greater than 100. I have changed the size of the icons such as bold, italics and when I run the program in 100 dpi, the icons sizes are bigger but when i run the program after changing to dpi greater than 100 the icons are getting smaller, and not updating to any size values . I have tried autosize = false, imagescaling to none.
Working with "System.Drawing.Icon" icons you should keep in mind to use a bigger size of the icon if you use DPI greater than 100. The property autosize does not help here.
The file of the icon can contain within different sizes so we can detect actual DPI scale factor and considering this factor to load the icon from the filesystem with the proper size.
The code for detecting DPI factor can look like:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public static class DpiHelper
{
private static readonly double m_dpiKoef = Graphics.FromHdc(GetDC(IntPtr.Zero)).DpiX / 96f;
public static double GetDpiFactor()
{
return m_dpiKoef;
}
[DllImport("User32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
}
Now using Icon(string fileName, int width, int height) from System.Drawing.Icon the initialization of the new instance of the icon can look like:
int size = 48;
int dpiSize = (int)(size * DpiHelper.GetDpiFactor());
Icon dpiIcon = new Icon(filename, new Size(dpiSize, dpiSize));

GMap.Net location on marker centralizes over point not above

I have just started using GMAP.Net and I'm Setting a custom Marker thus:
marker = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(latlon[0]), Convert.ToDouble(latlon[1])), new Bitmap(Iconpath));
where Iconpath points to a 42 * 38 pixel image of type PNG.
however the image appears central and immediately above the point being set by the above. What I would like is to know how to set it so the center of the image in over the location.
Any idea how to do that?
this is in a winforms .Net 4.0 application.
I found there was an easy way to do this thus:
Bitmap imgMarker = new Bitmap(Iconpath);
marker = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(latlon[0]), Convert.ToDouble(latlon[1])), imgMarker);
marker.Offset = new Point(imgMarker.Width/2, imgMarker.Height / 2);
hope it helps someone else!
The GoogleMarker class seems to be designed for push pin looking images, where you would want the pin tip to be directly above the point of interest. Your best bet is to inherit the marker class and make your own class where you can control the image placement. Like this:
class customImageMarker: GMapMarker
{
Bitmap Bitmap;
public customImageMarker(PointLatLng p, Bitmap Bitmap)
: base(p)
{
this.Bitmap = Bitmap;
Size = new System.Drawing.Size(Bitmap.Width, Bitmap.Height);
Offset = new Point(-Size.Width / 2, -Size.Height / 2);
}
public override void OnRender(Graphics g)
{
g.DrawImage(Bitmap, LocalPosition.X - Offset.X, LocalPosition.Y - Offset.Y, Size.Width, Size.Height);
}
}
Now just call your class:
marker = new customImageMarker(new PointLatLng(Convert.ToDouble(latlon[0]), Convert.ToDouble(latlon[0])), new Bitmap(Iconpath));
You can control the placement of the icon by adjusting the Offset variable.

Rendering a WPF canvas as a specificly sized bitmap

I have a WPF Canvas that I want to make a bitmap of.
Specifically, I want to render it actual size on a 300dpi bitmap.
The "actual size" of the objects on the canvas is 10 device independent pixels = 1" in real life.
Theoretically, WPF device independent pixels are 96dpi.
I've spent days trying to get this to work and am coming up flummoxed.
My understanding is that the general procedure is roughly:
var masterBitmap = new RenderTargetBitmap((int)(canvas.ActualWidth * ?SomeFactor?),
(int)(canvas.ActualHeight * ?SomeFactor?),
BitmapDpi, BitmapDpi, PixelFormats.Default);
masterBitmap.Render(canvas);
and that I need to set the canvas's LayoutTransform to a ScaleTransform of ?SomeOtherFactor? and then do a measure and arrange of the canvas to ?SomeDesiredSize?
What I am stuck on is what to use for the values of ?SomeFactor?, ?SomeOtherFactor? and ?SomeDesiredSize? to make this work. MSDN documentation gives no indication of what factors to use.
I use this code to display images with 1:1 pixel accuracy.
double dpiXFactor, dpiYFactor;
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
if (m.M11 > 0 && m.M22 > 0)
{
dpiXFactor = m.M11;
dpiYFactor = m.M22;
}
else
{
// Sometimes this can return a matrix with 0s.
// Fall back to assuming normal DPI in this case.
dpiXFactor = 1;
dpiYFactor = 1;
}
double width = widthPixels / dpiXFactor;
double height = heightPixels / dpiYFactor;
Don't forget to enable UseLayoutRounding on the control as well.

Resources