Xamarin SkiaSharp: Draw on different screen resolutions - xamarin.forms

My query is based on this post too
I draw on an image with list items to highlight specific item. I was developing the app testing on Samsung Galaxy Note 8 having resolution 1440 x 2960. It works for me as expected in Note 8. But when I tested with another device, the highlight goes weird. As the resolution changed, it goes to wrong place.
So what is the best way to do draw on image considering multiple screen resolutions.
canvas.DrawLine(150, 400, 1300, 400, thickLinePaint);
As the canvas.DrawLine receives the parameters in coordinates, how can convert to pixels so that the highlighting can work on all devices.
Please have a look at the below tests:
On Samsung Galaxy Note 8 ( 1440 x 2960 )
.
.
.
.
On Google Pixel 2 ( 1080 x 1920)
Edit:
When I inquired myImage.X & .Y, myCanvas.X & .Y, both gives 0. But as we see in the above screen shots, its not at 0.

Related

Matching an image pixel by pixel with the captured screen in asp.net window forms

I am working to find an image(3 x 3) in the screen captured(1366 x 768).
Actually, the screen captured is the browser's screenshot in which a website is opened.
I'm finding the image and calculating the X and Y coordinates.
I'm not facing any problem with the code it's working fine and giving me the exact X,Y coordinates where it finds the exact pixels(3 x 3) RGB values in the captured screen. Note that it finds the image by comparing the RGB value of each pixel.
The problem is that each website creating the different pixel format and my image(3 x 3) is working only with the specific website. I want to make it work with all websites.
This is the image I need to find:
Below you can see that two different websites have the different pixel format for the same word.
--> The below both screenshots were taken by the process -> open a website -> ctrl + f -> type "Home"
--> Written RGB Values are only of Green color.
It's able to find the above image(3 x 3) in the below one:
It's NOT able to find the above image(3 x 3) in the below one. Because the pixel format is completely different.
Is there any way to override this problem?
I can create a new image(3 x 3 or of any size) to find in the captured screen. But that should work with all websites.

Relation between dp - sp and PX

I am not asking what is the difference between dp,sp and px.
I am designing a website based on google's new material design, all the measurements are in dp (for grid) and sp(for text). My question is how do they translate to pixels. I have been designing websites for more than 4 years and all the measurements (grid and font) are in pixels.
For Example:
A headline is 24sp, how many pixels does it equate to? (its not 24px, I've tried matching them, it's around 28px but there has to be a standard measuring systems).
Grid Guideline: "All components align to an 8 dp square baseline grid." - how many pixels does it equate to?
1px = ?dp =?sp on a desktop or any average monitor or mobile device?
I recommend reading Google's definitions of dp and sp, which can be found in the Android docs, here and here.
There's also some helpful information in the wonderful Designer's Guide to DPI.
I think the answer is going to be:
1px = 1dp = 1sp on any average monitor or mobile device.
How did I come up with this?
Because a pixel is a pixel, for andriod dp and sp are used because they are used for native apps which have to scale and the dpi of each screen is different based on device. For desktops all of this is same, off course the website has to be compatible/responsive for mobile devices but since the website loads in a browser, some additional media quires (based on guidelines) will do the job.
If anybody has some other logical conclusion, please share
A safe rule of thumb is to use 1 px = 1 dp.
This should give you a good safe size on just about any device. It will appear a bit large on some devices, notably the iPad (regular).
Here's why:
"A dp corresponds to the physical size of a pixel at 160 dpi" (https://developer.android.com/training/multiscreen/screendensities.html#TaskUseD)
160 dpi means:
160 dots = 1 inch
Therefore:
160 dp = 1 inch (25.4 mm)
So when Google recommends that buttons have a touchable target height of 48 dp, they're saying that they need to be 0.3 inches (7.6 mm) tall.
So how many px is this? Well, that depends on the device.
Examples for 48 dp (7.6 mm) button height:
iPad mini: 48 px Why: The iPad mini screen is about 120 mm wide and uses 768 px to fill that space. You therefore need 162 px to take up an inch (25.4 mm), or 48 px for your button height of 7.6 mm.
Kindle Fire (7"): 43 px
Kindle Fire (6"): 50 px
iPhone: 48 px
Nexus 7: 48 px
Regular iPad: 39 px
(I may have fudged the rounding up/down a tiny bit.. I like 48 better than 49!)
Screen mm and CSS px width for examples: I calculated the screen width using the CSS px screen dimensions and diagonal length.
iPad mini: 1024 x 768 resolution and 201 mm diagonal = 120 mm width.
Kindle Fire 7": 858 x 533 resolution and 178 mm diagonal = 94 mm width.
Kindle Fire 6": 853 x 533 resolution and 152 mm diagonal = 81 mm width.
iPhone: 568 x 320 resolution and 102 mm diagonal = 50 mm width.
Nexus 7: 960 * 600 resolution and 178 mm diagonal = 94 mm width.
iPad regular: 1024 x 768 resolution and 246 mm diagonal = 148 mm width.
Note that for calculating the px height of the button you need to use the device CSS px dimensions. These numbers are not necessarily the same as the resolutions stated in the specs.
All are roughly equivalent for most use cases.
Source
To preserve the visible size of your UI on screens with different densities, you must design your UI using density-independent pixels (dp) as your unit of measurement. One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density). Android translates this value to the appropriate number of real pixels for each other density.
For example, consider the two devices in figure 1. If you were to define a view to be "100px" wide, it will appear much larger on the device on the left. So you must instead use "100dp" to ensure it appears the same size on both screens.
When defining text sizes, however, you should instead use scalable pixels (sp) as your units (but never use sp for layout sizes). The sp unit is the same size as dp, by default, but it resizes based on the user's preferred text size.
Figure 1. Two screens of the same size may have a different number of pixels

Retrieve resolution of an uploaded image using ImageResizer

Does ImageResizer.Net for asp.net have any method by which we can know the resolution of any uploaded Image. I know one method:
ImageResizer.ImageBuilder.LoadImageInfo (object source, IEnumerable< string > requestedInfo )
that gives me Height, Width, Mimetype & extension of image file, but I have a requirement where I need to validate the resolution of an uploaded image.
As far as I know dpi is something what connected to devices where images will be rendered.
So, if it is Desktop dpi can be or 72 (MAC) or 96 (Windows), but if you will print it dpi can be 150 or 300.
So to check if image will be correctly printed (with good quality) on paper with size 200x200 milimeters you can calculate min size if image you need in pixels.
So, we have the following input:
- dpi of printing is 150
- size of paper is 200x200 milimeters
200 mm is 7.9 inch
=> min resolution of image should be 7.9*150 = 1185 pixels
So only images with height and width equal or more than 1185 will be printed with good quality.
So question for you - what for you need resolution(dpi) check?
As far as you have Height and Width you can calculate DPI using this formula:
dp = sqrt(width^2 + height^2) (Pythagorean theorem)
then, you should know size of the screen in inches (di). Simply:
dpi = dp / di

how to draw 2 centimeters long line on screen in adobe air

I need to draw 2 centimeters long line on screen on an Adobe Air application. I don't know how to do it!
Explanation:
I am getting parameters from another application say x centimeters, and that parameter is in centimeters.
I need to draw a circle exactly x centimeters from the top of the screen.
best regards
If I remember correctly, you won't be able to do it on desktop since AIR always returns 72DPI for the screen (I may be incorrect on that point, however). It is fairly easy to do on mobile, though, assuming AIR returns the proper DPI (retina iPads did not return the correct DPIs prior to AIR 3.3, I believe).
Basically, you convert inches to pixels simply by multiplying by the DPI.
var dpi:Number = Capabilities.screenDPI; //unnecessary to save local version, just easier to reference
var heightCM:Number = 5;
var widthCM:Number = 5;
var widthPixels:Number, heightPixels:Number;
var heightIn:Number = cmToInches( heightCM );
var widthIn:Number = cmToInches( widthCM );
widthPixels = widthIn * dpi;
heightPixels = heightIn * dpi;
function cmToInches( value:Number ):Number {
return value * .393701;
}
That will take a size (I built it for height and width, but you can adapt it to your needs) in centimeters, convert it to inches, and then convert it to pixels. You'd obviously want to turn that into a neat static Util method, but it would do the trick.
If you want, I created a Flex application last year to try and understand how AIR handles DPI differences. It just draws a red rectangle to a specific size on screen using on-screen sliders to determine the size (in inches). I don't have it here at work, but I could post the code when I get home.
Again, I do not believe this will work in desktop applications due to AIR always reporting 72 DPI. I hope I am wrong, but I do not believe I am.

why my iphone device and simulator has different screen resolution?

i use itouch 4G has my device and i use simulator-4.2
i will just draw a rectangle as an example. i use Quartz-2d to draw
- (void)drawRect:(CGRect)rect {
// Get a graphics context, saving its state
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Reset the transformation
CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context,t0);
// Draw a green rectangle
CGContextBeginPath(context);
CGContextSetRGBFillColor(context, 0,1,0,1);
CGContextAddRect(context, CGRectMake(0,0,320,480));
CGContextClosePath(context);
CGContextDrawPath(context,kCGPathFill);
CGContextRestoreGState(context);
}
i run it in the simulator, the whole screen becomes green, then i run this on my device, only the quarter of the screen becomes green, in order to make the whole screen green on my device, i have to draw a larger rectangle
CGContextAddRect(context, CGRectMake(0,0,640,960));
seem like my device has twice resolution than the simulator,
how can i fix this?
The Retina display on the iPhone is twice the resolution of the previous generation of phones. Your simulator is probably running using the 'iPhone' device rather than the 'iPhone4' device. You can switch in the Hardware | Device menu.
You can get the current scale of view you're rendering to with
[self.layer contentsScale]
then scale your dimensions accordingly.

Resources