问题
I'm trying to follow this article and it was easy to implement text over image
and now my problem is in the above mentioned article the image watermark was placed 10 pixels from left so how do I place image similarly to top right,top middle,middle left, center,middle right and similary to bottom.
Here is how it was placed to the top right corner :
int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10;
grWatermark.DrawImage(
imgWatermark,
new Rectangle(
xPosOfWm, yPosOfWm,
wmWidth, wmHeight
),
0, 0,
wmWidth, wmHeight,
GraphicsUnit.Pixel,
imageAttributes
);
回答1:
The problem is that you will have to calculate your image height and width first
calculate the original Image height and width
Image oImage="path";
var oheight=oImage.Height;
var oWidth=oImage.width;
Now Calculate the Image which you want to place over it
var WmImage="path";
var wWheight=WmImage.Height;
var wWidth=WmoImage.width;
top-right
var left=oWidth-wWidth-10;
var top=oheight-10;
//draw the wate mark image on thse point
oImage.DrawImage(imgWatermark,new Rectangle(left,top,wmWidth,
wmHeight),0,0,wmWidth,wmHeight,GraphicsUnit.Pixel,imageAttributes);
similarly you can calculate for other images alos.
回答2:
The current code doesn't place the watermark at the top left, it places at the top right.
To place it at the top left, you use:
int xPosOfWm = 10;
int yPosOfWm = 10;
To position the watermark horisontally at the left, center and right:
int xPosOfWm = 10;
int xPosOfWm = (phWidth - wmWidth) / 2;
int xPosOfWm = (phWidth - wmWidth) - 10;
To position the watermark vertically at the top, middle and bottom:
int yPosOfWm = 10;
int yPosOfWm = (phHeight - wmHeight) / 2;
int yPosOfWm = (phHeight - wmHeight) - 10;
Just combine one horisontal with one vertical to get any combination that you want.
来源:https://stackoverflow.com/questions/14794290/watermarking-image-positionasp-net