c# - Graphics.DrawImage unexpectedly resizing image -
i've got code takes png greyscale image transparency , attempts create new image has given background colour (looked database) , overlays original image on create image of required colour highlights , shading. code run in asp.net context don't think relevant.
the code works fine on local computer when deployed our uat server gives unexpected results. on uat creates image of right size shaded/highlighted area seems have been shrunk in each dimension 20%. main image looking @ 5x29 , output image 5x29 shaded area 4x23.2 (the 24th row different background colour assumed doing interpolation resize).
my code failing follows:
private byte[] getimagedata(cachekey key) { byte[] imagedata; using (image image = image.fromfile(key.filepath)) using (bitmap newimage = new bitmap(image.width, image.height)) { using (graphics graphic = graphics.fromimage(newimage)) { using (solidbrush brush = new solidbrush(colortranslator.fromhtml(key.backgroundcolour))) { graphic.fillrectangle(brush, 0, 0, image.width, image.height); } graphic.drawimage(image, 0, 0); /* following lines see if there transparency mask create final transparency. using getpixel , setpixel , modifying alpha of newimage alpha of mask. don't think should make difference code below anyway. */ bitmap mask; if (trygetmask(key.filepath, out mask)) { applymask(newimage, mask); } using (var memorystream = new memorystream()) { newimage.save(memorystream, imageformat.png); imagedata = memorystream.toarray(); } } } return imagedata; } private void applymask(bitmap bitmap, bitmap mask) { if (mask.width != bitmap.width || mask.height != bitmap.height) { throw new argumentexception("bitmap sizes not match"); } (int y = 0; y < bitmap.height; y++) { (int x = 0; x < bitmap.width; x++) { color colour = bitmap.getpixel(x, y); colour = color.fromargb(mask.getpixel(x, y).a, colour); bitmap.setpixel(x, y, colour); } } }
here images getting (repeated 4 times better demonstrate issue). first correct image getting local computer. second turning uat server exhibiting strange "reduced 20%" issue. being used in similar way repeating background can see why effect noticeable. these generated white background make easiest see issue. i've got similar images in other colours if wants them. :)
as final clarification images being used should identical (the uat deployed checked our git repro , there has never been more 1 version of these images can't using wrong version.
i thinking maybe underlying gdi doing different on server on computer can't think or why.
any explanations behaviour or better yet fixes appreciated. otherwise i'm going have go , manually pixel pixel doing transparency , overlay myself seems bit silly.
graphic.drawimage(image, 0, 0);
it's not strange when use overload of drawimage(). try display image @ original physical size. affected dpi (dots per inch) setting of video adapter. common settings today 96, 120 , 144 dpi, easy change display applet in windows. these dpi values correspond 100%, 125% , 150% in applet.
if want make sure not happen , exact size in pixels you'll need use drawimage(image, rectangle) overload.
do note physical size matters. if ever use program on "retina" monitor, day getting closer , closer, 5x29 pixel image going fleck of dust on nice display. making programs dpiaware has been ignored past 30 years getting important.
Comments
Post a Comment