Click here to Skip to main content
14,739,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys ...
i have an algorith to water mark specific photo and it work good but when i use specific photo it fail to Save it
what do you think that make my algorithm to Water mark Some photos and others not and what is the solution...?
thanks
you should know my algorithm go through these steps
firs i recieve the photo and create a new bitmap and save this coming photo to the bit map water mark it the Save it
...
the Water Mark code is
#region Defaults
//define a string of text to use as the Copyright message
string Copyright = Str_annotaion;
//create a image object containing the photograph to watermark
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(this.fupload.FileName.ToString());
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);
#endregion
#region insert Watermark textMessage
//create a image object containing the watermark
//System.Drawing.Image imgWatermark = new Bitmap(WorkingDirectory + "\\watermark.bmp");
//int wmWidth = imgWatermark.Width;
//int wmHeight = imgWatermark.Height;
//------------------------------------------------------------
//Step #1 - Insert Copyright message
//------------------------------------------------------------
//Set the rendering quality for this Graphics object
grPhoto.SmoothingMode = SmoothingMode.HighQuality;
//Draws the photo Image object at original size to the graphics object.
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new System.Drawing.Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure
//-------------------------------------------------------
//to maximize the size of the Copyright message we will
//test multiple Font sizes to determine the largest posible
//font we can use for the width of the Photograph
//define an array of point sizes you would like to consider as possiblities
//-------------------------------------------------------
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
//Loop through the defined sizes checking the length of the Copyright string
//If its length in pixles is less then the image width choose this Font size.
for (int i = 0; i < 7; i++)
{
//set a Font object to Arial (i)pt, Bold
crFont = new Font("arial", sizes[i]);
//Measure the Copyright string in this Font
crSize = grPhoto.MeasureString(Copyright, crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
//Since all photographs will have varying heights, determine a
//position 5% from the bottom of the image
int yPixlesFromBottom = (int)(phHeight * .05);
//Now that we have a point size use the Copyrights string height
//to determine a y-coordinate to draw the string of the photograph
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
//Determine its x-coordinate by calculating the center of the width of the image
float xCenterOfImg = (phWidth / 2);
//Define the text layout by setting the text alignment to centered
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Near;
//define a Brush which is semi trasparent black (Alpha set to 153)
SolidBrush semiTransBrush2 = new SolidBrush(System.Drawing.Color.FromArgb(100, 255, 255, 255));
//Draw the Copyright string
grPhoto.DrawString(Copyright, //string of text
crFont, //font
semiTransBrush2, //Brush
new PointF(XAxis,Yaxis), //Position
StrFormat);
//define a Brush which is semi trasparent white (Alpha set to 153)
SolidBrush semiTransBrush = new SolidBrush(System.Drawing.Color.FromArgb(153, 255, 255, 255));
//Draw the Copyright string a second time to create a shadow effect
//Make sure to move this text 1 pixel to the right and down 1 pixel

Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;
grPhoto.Dispose();
//save new image to file system.
return imgPhoto;
//imgPhoto.Dispose();
//imgWatermark.Dispose();
#endregion
Posted
Updated 1-Dec-10 2:36am
v4

1 solution

Your question is somewhat vague because there is no example code with some info on where it fails.

Try to check out some stuff like:
- What is different between working and nonworking pictures?
- Width?
- Height?
- Can you save it without adding the watermark
- Other changes?

Good luck!
   
Comments
Yasser El Shazly 1-Dec-10 7:25am
   
yes it Save but no changes Saved on some photo but others Saved with changed Well can you gues why..?
E.F. Nijboer 1-Dec-10 11:26am
   
As you mention... it's guessing...
Maybe resizing is not going well. Mayne check out the dpi of images that succeed and don't succeed. You need to compare all the properties of a working and non working image to get an idea of what the problem might be. Also, have a look at the link for a proper resize example.
http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

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