PDFsharp drawing images at 72 DPI, if more resolution it is drawn "smaller" - pdfsharp

I'm drawing an image of 100 x 100 pixels in a PDFSharp document.
The image is drawn using:
g.DrawImage(image, new Point(x, y));
The issue here is that if I draw a rectangle starting in the same coordinates x,y and using 100x100 as dimensiones... the rectangle is larger than the image.
If I use the other overloads in DrawImage, setting the container rectangle, the image fits the rectangle but it loses quality (it is enlarged).
I think it is a problem of different resolutions or something like that.
Any ideas?
UPDATE: I resized the image to 133x133 to fit the 100x100 rectangle. What is the reason of this difference? A 33% difference.
MY SOLUTION: When retrieving the image and scaling it to fit the rectangle, you need to take into account that the size of your image is in PIXELS and when you draw in the PDF it is in points. If your image is in 96 DPI, you need to increase its dimension multiplying by "96/72" (that is the 33% I got), and that way you will see what you expect (even if you draw it using a container rectangle or just the starting coordinates).
Set image.Interpolate = false; to disable anti-aliasing and increase the sharpness of the small image.

There are no "pixels" in PDF. DrawImage has overloads that allow to draw the image with a specific size.
If the size is omitted (as you do), the size will be determined by the DPI setting of the image. Could it be your image is set to 96 DPI?
The rectangle 100x100 uses points - and there are 72 points per inch.
The image does not lose quality when you set the size. The "quality loss" depends on the zoom level of the viewer.
You can set a hint to prevent Adobe Reader from anti-aliasing the image.
Update:
Set image.Interpolate = false; to disable anti-aliasing.

Related

How to resize and reposition PaperJs drawing with canvas size change?

I'm creating an application using Paperjs to drawing some sketch on an image, persisting the sketch without the background and display back the sketch along with background image.
In my Paperjs canvas, I have a raster image and on top of that I'm making some sketch pointing a part of the background image. After the drawing is done, I'm doing JSON export, excluding background raster image, to persist the drawing. Now when I export it back, the background raster image is generated dynamically and the Paper canvas size is set to the size of the image. But if the browser window size is different then the size of the image will be different than where the original drawing is created.
The issue that I'm facing if the image size isn't the used while creating the sketch, its pointing to the different part of the image. Is there any way we can proportionally change the paperjs drawing so that the sketch points to the same point of the background raster image?
Any help would be appreciated.
Below is an example :
With actual size -
After resize
I'm able to achieve what I wanted to with the below changes. Thanks #arthur.sw for your help.
Here originalViewSize is the original size on which the actual sketch was created.
Steps:
Update view size
Calculate x-axis and y-axis scale factor
Update bounds for background image
Update active layer positions
Scale active layer
// Changing view size and calculating scale factor for x & y-axis
paperScope.view.viewSize = new Size(originalViewSize.width + 200, originalViewSize.height + 100);
const xScale = paperScope.view.viewSize.width /originalViewSize.width;
const yScale = paperScope.view.viewSize.height /originalViewSize.height;
// Update b/g image bounds
backgroundRaster.bounds = paperScope.view.bounds;
paperScope.project.activeLayer.position.x = paperScope.project.activeLayer.position.x * xScale;
paperScope.project.activeLayer.position.y = paperScope.project.activeLayer.position.y * yScale;
paperScope.project.activeLayer.scale(xScale, yScale);

How to ensure even pixel size when painting scaled QImage?

I have a 4 channel png image with 8x8 Pixels that is loaded by a QImage. The QImage is then scaled by a factor of 200, so the image will have a new resolution of 1600x1600, each original pixel having a size of 200x200. But when this image is added to a QLabel through the means of a QPixmap and shown on screen, the drawn pixels will have slightly different sizes.
I've taken screenshots with Gimp and looked at the painted image more closely. It seems that every other pixel is slightly bigger than it should be, 201 instead of 200 pixels wide for example. The very last pixel in a row will then be smaller to compensate, so that the entire image has the correct size in the end.
This does not happen for all scaling factors, 100 is fine for example and so are factors that are a power of 2, such as 256.
My original approach was using a QGraphicsView and a QGraphicsPixmapItem in which case I scaled the GraphicsItem instead of the image. The effect was the same.
What effect am I seeing here? And what, if anything, can be done about it?
The code to reproduce this issue is very straightforward
int scale = 200;
image = QImage("some image file");
QPixmap pixmap = QPixmap::fromImage(image.scaled(image.size() * scale));
some_label->setPixmap(pixmap);
Turns out the easiest solution to my problem is to use QOpenGLWidget in the QGraphicsView:
setViewport(new QOpenGLWidget);
This single line in the constructor will result in much higher precision when scaling an image with the caveat of adding OpenGL as a dependency.
Another gotcha with this approach is that calling setViewport invalidates many of the settings done on a QGraphicsView. So if the view is set up in a UI file, as in my case, make sure to call other setters after calling setViewport.
I could not find a better solution that would work without OpenGL, short of writing my own rasterizer of course.

How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio

I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
I tried this but nothing happened:
ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);
You are providing the rectangle of the view and not that of the scene.
This should work:
ui->graphicsView->fitInView(scene->itemsBoundingRect() ,Qt::KeepAspectRatio);
itemsBoundingRect calculates and returns the bounding rect of all items on the scene. So the graphicsview's view matrix will be scaled in order to fit the contents of the scene.
I would advise you to reimplement resizeEvent and have this call there as well.

How to scale and clip a BitmapImage in Flex 4.5

Here's my problem, I need to scale and clip images into square sized tiles to put into a tile list. Here's how I want it to work:
I want all my tiles to be, say, 300px x 300px.
For each image, I want to scale the shorter side (either width or height) to fit in the tile using the "letterbox" scaleMode (so that it maintains aspect ratio).
Then I want to position the image in the center and clip away anything left over from either both sides or the top and bottom.
Here's an example to help clarify:
I have an image with width=600px and height=1200px. First I want to scale the image to width=300px and height=600px (notice that aspect ratio is maintained), then center the image vertically and clip the image to 300 x 300.
Is this possible? This is actually a pretty standard way of displaying square thumbnails in many photo-based web sites, but I can't find a way to make it work in flex.
Any help would be appreciated, thanks!
UPDATE JUNE 2012:
Just in case anyone finds this thread now, this issue has been resolved in the latest version of the Flex SDK. On the spark image object there is a new scaleMode of "zoom" which does exactly what I've asked for here.
Take your big image and draw it on BitmapData with scale and reposition:
const zoom:Number = Math.max(THUMB_WIDTH/image.width, THUMB_HEIGHT/image.height);
const x:int = (THUMB_WIDTH - image.width*zoom)/2;
const y:int = (THUMB_HEIGHT - image.height*zoom)/2;
var matrix:Matrix = new Matrix;
matrix.scale(zoom, zoom);
matrix.translate(x, y);
var _thumbBitmap:BitmapData = new BitmapData(THUMB_WIDTH, THUMB_HEIGHT, false, 0xFFFFFF);
_thumbBitmap.draw(image, matrix, null, null, null, true);
Then assign resulting BitmapData to the source of the BitmapImage.
More: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#draw%28%29
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/primitives/BitmapImage.html#source

Handling inverted pixels in bitmapData and bitmap class in as3

I am using bitmapData and bitmap classes to render a mouse cursor on the display screen. The bitmapData consists of an area whose colors should be inverted according to the background color. This is a very basic thing which could be observed with text cursor(the vertical line with two small horizontals on top and bottom), when moved over the text area.
I want to be able to do the same with the pixels in my bitmapData, is there a way to find out the background color effectively and invert the color values?
In this process i will be redrawing the whole pixels, is there any other efficient way to do that ?
You can draw your cursor using BlendMode.INVERT
http://livedocs.adobe.com/flex/3/langref/flash/display/BitmapData.html#draw()
or simply put your cursor display object over your bitmap and set it's blendMode to INVERT.

Resources