| 发表于:2007-01-24 15:42:082楼 得分:50 |
框选 private graphicspath gp = new graphicspath(); mousemove: preview.refresh(); gp.reset(); gp.addrectangle(new rectangle(p0.x, p0.y, math.abs(p0.x - p1.x), math.abs(p0.y - p1.y))); graphics g = preview.creategraphics(); pen pen = new pen(color.red, 1.0f); g.drawpath(pen, gp); g.dispose(); mousedown: preview.refresh(); gp.reset(); 需要注意处理起始点和终止点的位置问题(比如,用户从右下拖到左上) 框选好以后你可以用graphics类的drawimage得到框选好的图像。同样还是drawimage实现放大缩小。 //剪裁 public static bitmap kicut(bitmap b, int startx, int starty, int iwidth, int iheight) { if (b == null) { return null; } int w = b.width; int h = b.height; if (startx > = w ¦ ¦ starty > = h) { return null; } if (startx + iwidth > w) { iwidth = w - startx; } if (starty + iheight > h) { iheight = h - starty; } bitmap bmpout = new bitmap(iwidth, iheight, pixelformat.format24bpprgb); graphics g = graphics.fromimage(bmpout); g.drawimage(b, new rectangle(0, 0, iwidth, iheight), new rectangle(startx, starty, iwidth, iheight), graphicsunit.pixel); g.dispose(); return bmpout; } //重设大小 public static bitmap kiresizeimage(bitmap bmp, int neww, int newh) { try { bitmap b = new bitmap(neww, newh); graphics g = graphics.fromimage(b); //强制用效果最好的插值算法 g.interpolationmode = interpolationmode.highqualitybicubic; g.drawimage(bmp, new rectangle(0, 0, neww, newh), new rectangle(0, 0, bmp.width, bmp.height), graphicsunit.pixel); g.dispose(); return b; } catch { return null; } } 需要的话,最后再把得到的bitmap重新画到picturebox里。 | | |
|