How to Add a Watermark to an Image Using This Code

How can I add a watermark to an image using this code?

If you grab the UIImageViews' images you could use the following concept:

if let img = UIImage(named: "image.png"), img2 = UIImage(named: "watermark.png") {

let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)

UIGraphicsBeginImageContextWithOptions(img.size, true, 0)
let context = UIGraphicsGetCurrentContext()

CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, rect)

img.drawInRect(rect, blendMode: .Normal, alpha: 1)
img2.drawInRect(CGRectMake(x,y,width,height), blendMode: .Normal, alpha: 1)

let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil)

}

Add 'Watermark' to images with php

A good example in the PHP manual:

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

add watermark image to uploaded image

I tested this using the code below and this worked ok. Obviously I have used paths relevant to my test system but hopefully it should help.

One of the most important and often forgotten things when uploading and dealing with uploaded files is the enctype of the form - so I included my test form as an example.

If you want to save the image as well as display it with the watermark use the imagepng function twice, once with a filename and the other without.

<form method='post' action='/test/so/wtrmarkimg.php' enctype='multipart/form-data'>
<h1>Image uploader - Watermark</h1>
<input type='file' name='image' />
<input type='submit' value='Submit' />
</form>

<?php

#$path = "../large/";

$path='c:/temp/';/* output path for images generated */
$watermarksrc=realpath( 'c:/wwwroot/images/watermark.png' );

if( isset( $_FILES['image'] ) ){

$img_tmpname=$_FILES['image']['tmp_name'];


$num = substr( md5( mt_rand( 1,9999999999 ) ),0,9);
$new_name = $path.$num.".jpg";
$image = $num.".jpg";

if( move_uploaded_file( $img_tmpname, $new_name ) ){

$image = imagecreatefromjpeg( $new_name );
$logoImage = imagecreatefrompng( $watermarksrc );
imagealphablending( $logoImage, true );

$imageWidth=imagesx($image);
$imageHeight=imagesy($image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);

imagecopy(
$image,
$logoImage,
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
0, 0,
$logoWidth, $logoHeight );

// Set type of image and send the output
header("Content-type: image/png");
imagepng( $image );/*display image with watermark */
@imagepng( $image, $new_name );/* save image with watermark */

// Release memory
imagedestroy( $image );
imagedestroy( $imageLogo );
}
} else {
echo "ERROR";
print_r($_FILES);
}
?>

Add watermark image in android

it solved , I just change little for that code , and thanks for ur advice Doomsknight :)

 public static Bitmap mark(Bitmap src, String watermark) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(18);
paint.setAntiAlias(true);
paint.setUnderlineText(true);
canvas.drawText(watermark, 20, 25, paint);

return result;
}

and I call with this function

bitmap = mark(bitmap, "Hallo");
imgshoot.setImageBitmap(bitmap);

Automatically add watermark to an image

Finally i find the solution to my question...

The corrected code answer is following

    private void button1_Click(object sender, EventArgs e)
{
using (Image image = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
using (Image watermarkImage = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\watermark.png"))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width+1, watermarkImage.Height)));
image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
}

}

my thanks to Furqan Safdar and Abdias Software
The link Problem in tiling image starting at different height using TextureBrush in C# also helped me to solve this problem

and thanks all

finally result

Sample Image



Related Topics



Leave a reply



Submit