Thursday 15 May 2014

c# - Cropping images sometimes a wrong area -



c# - Cropping images sometimes a wrong area -

i'm using jquery-cropbox crop images on web application.

everything running smoothly of time, time time, generated image wrong, if cropping applied wrong coordinates.

as example, consider image :

in cropbox, zoom , drag until :

but when click on crop button, gives me this:

here cropping code:

private bitmap cropimage(image img, rectangle rect) { bitmap newimg = new bitmap(rect.width, rect.height); graphics g = graphics.fromimage(newimg); g.drawimage(img, -rect.x, -rect.y); homecoming newimg; } //... // save image var streamout = new memorystream(); bitmap newimg = cropimage(oldimg, rect); newimg.save(streamout, imageformat.png);

oldimg image posted, rect coordinates utilize crop.

i checked coordinates in rect of course, correct.

for reference, obtain result provided coordinates (those may not accurate, took them sec test, placed image bit differently):

x: 47 y: 94 w: 150 h: 150

what happening ?

your image (as posted) has 72dpi; screen , hence graphics utilize have 96dpi.

this lead effect see.

to avoid effect set resolution of img before drawing 1 in graphics object:

img.setresolution((int)g.dpix, (int)g.dpiy);

c# image graphics png crop

No comments:

Post a Comment