Click here to Skip to main content
15,946,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below code I would like to make sure that the water mark image does not get resize with the cropped image. I would like the water mark image to remain it;s original size. Any help would be appreciated.
C#
// Thumbnail-size image
                   Byte[] byteBLOBData = new Byte[0];

                   byteBLOBData = (byte[])dReader["p_Image"];

                   ImageExtension = dReader["p_Type"].ToString();

                   MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);

                   stmBLOBData.Write(byteBLOBData, 0, byteBLOBData.Length);
                   System.Drawing.Image returnImage = System.Drawing.Image.FromStream(stmBLOBData, false, false);

                   //define a string of text to use as the Copyright message
                   string Copyright = "Copyright © 2011 - OmegaLove.com";
                   //add fading to logo image  using TextureBrush
                   System.Drawing.Image waterMarkimage = System.Drawing.Image.FromFile (
                       (System.Web.HttpContext.Current.Server.MapPath("~/Images/Watermark/watermark.jpg")));
                   System.Drawing.Image wm = Watermark.SetOpacity(waterMarkimage, 150);
                   System.Drawing.Image cropImage = ImageHandler.ImageCrop(returnImage, 100, 160,
                       ImageHandler.AnchorPosition.Bottom);
                   Graphics g = System.Drawing.Graphics.FromImage(cropImage);
                   Point point = new Point(cropImage.Width / 3, cropImage.Height / 2);
                   g.DrawImage(wm, point);


                   // Set the page's content type to JPEG files
                   // and clear all response headers.
                   context.Response.ContentType = "image/jpeg";
                   context.Response.Clear();

                   // Buffer response so that page is sent
                   // after processing is complete.
                   context.Response.BufferOutput = true;

                   // Save the bitmap to the response stream and
                   // convert it to JPEG format.
                   cropImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                   waterMarkimage.Dispose();
                   wm.Dispose();
                   g.Dispose();
                   // Release memory.
                   cropImage.Dispose();
                   // Send the output to the client.
                   context.Response.Flush();
Posted
Updated 15-Feb-11 17:16pm
v2

1 solution

Is there a way where you can resize the image first and then add the watermark to the file?
The watermark will get resized unless you add it as a different layer on the image.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 23:57pm    
Simple logic shows it is possible (look at the codes: a watermark is created in the code), so this is a way to go, my 5.
--SA
Abhinav S 16-Feb-11 0:09am    
Thanks SA.
macupryk 16-Feb-11 10:30am    
this is true, but I am stuck on this.
Sergey Alexandrovich Kryukov 16-Feb-11 20:39pm    
I can't see where you stuck. Abhinav gave you a working idea; and you already have the code or putting a fixed-size watermark; but you did not show where the problem is. Maybe, this is because we don't know where in you code is re-sized and what triggers that. Before you complained that you stuck, I did not see the problem, but as you're puzzled, may be the code WHICH IS NOT SHOWN is a problem.
--SA
macupryk 16-Feb-11 21:56pm    
the back ground is black. I can see the watermark.

// Thumbnail-size image
Byte[] byteBLOBData = new Byte[0]; // Get image from database in byte type

byteBLOBData = (byte[])dReader["p_Image"];

ImageExtension = dReader["p_Type"].ToString(); // get type of image

MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);

stmBLOBData.Write(byteBLOBData, 0, byteBLOBData.Length); // Load image into stream

// Grab initial image
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(stmBLOBData, false, false);
////////////////////////////////////////////////////////////////////////////////////////////////////////



// resize initial image
System.Drawing.Image imgPhoto = ImageHandler.ImageCrop(returnImage, 100, 160, ImageHandler.AnchorPosition.Bottom);

int phWidth = 100;
int phHeight = 160;


//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // new resized bmp
////////////////////////////////////////////////////////////////////////////////////////////////////////


//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);


//create a image object containing the watermark
System.Drawing.Image imgWatermark = null;
int wmWidth = 0;
int wmHeight = 0;
imgWatermark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("~/Images/Watermark/watermark.jpg"));
wmWidth = imgWatermark.Width;
wmHeight = imgWatermark.Height;
/////////////////////////////////////////////////////////////////////////////////////////////


//------------------------------------------------------------
//Step #2 - Insert Watermark image
//------------------------------------------------------------
Bitmap bmWatermark = null;
Graphics grWatermark = null;

////Create a Bitmap based on the previously modified photograph Bitmap
bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
////Load this Bitmap into a new Graphic Object
grWatermark = Graphics.FromImage(bmWatermark);

//To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes();

//The first step in manipulating the watermark image is to replace
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
//to do this we will use a Colormap and use this to define a RemapTable
ColorMap colorMap = new ColorMap();

//My watermark was defined with a background of 100% Green this will
//be the color we search for and replace with transparency
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(255, 0, 0, 0);

ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(r

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900