c# - Translate/Detect Rectangle portion of Image from Resized Image -
i have large size image.since takes long process high res images resize keeping aspect ratio.from resized image detect rectangle , have coordinates of rectangle.
bitmap resizekeepaspectratio(bitmap imgphoto, int width, int height) { int sourcewidth = imgphoto.width; int sourceheight = imgphoto.height; int sourcex = 0; int sourcey = 0; int destx = 0; int desty = 0; float npercent = 0; float npercentw = 0; float npercenth = 0; npercentw = ((float)width / (float)sourcewidth); npercenth = ((float)height / (float)sourceheight); if (npercenth < npercentw) { npercent = npercenth; destx = system.convert.toint16((width - (sourcewidth * npercent)) / 2); } else { npercent = npercentw; desty = system.convert.toint16((height - (sourceheight * npercent)) / 2); } int destwidth = (int)(sourcewidth * npercent); int destheight = (int)(sourceheight * npercent); bitmap bmphoto = new bitmap(width, height, pixelformat.format24bpprgb); bmphoto.setresolution(imgphoto.horizontalresolution, imgphoto.verticalresolution); graphics grphoto = graphics.fromimage(bmphoto); grphoto.clear(color.red); grphoto.interpolationmode = interpolationmode.highqualitybicubic; grphoto.drawimage(imgphoto, new rectangle(destx, desty, destwidth, destheight), new rectangle(sourcex, sourcey, sourcewidth, sourceheight), graphicsunit.pixel); grphoto.dispose(); return bmphoto; } is there way can translate/map rectangle large image same area.im doing save time.
some clarification: have large original image.. resize keeping aspect ratio , using processing rectangle portion in it( coordinates).since image quality of portion not need find way map coordinate large image.
ok, if understand clear here is:
you have viewport in select rectangle , want scale rectangle unscaled image.
so have function this:
public rectanglef translatescale(rectanglef croprectangle, bitmap imgphoto) first of need calculate multiplier fit image on viewport, function:
int sourcewidth = imgphoto.width; int sourceheight = imgphoto.height; int sourcex = 0; int sourcey = 0; int destx = 0; int desty = 0; float npercent = 0; float npercentw = 0; float npercenth = 0; npercentw = ((float)width / (float)sourcewidth); npercenth = ((float)height / (float)sourceheight); if (npercenth < npercentw) npercent = npercenth; else npercent = npercentw; now know scale percentage take inverse of function, instead of multiply divide rectangle size , position:
croprectangle.x /= npercent; croprectangle.y /= npercent; croprectangle.width /= npercent; croprectangle.height /= npercent return croprectangle; and that's it, have rectangle scaled original image size, can crop rectangle.
Comments
Post a Comment