Front-endCode:
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > Asp.net uploads images and adds watermarks </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div ID = "TOP" >
ASP. NET upload and add watermarks
</ Div >
< Div ID = "Content" >
Select Upload image: < ASP: fileupload ID = "Upfiletest" Runat = "Server" /> < BR />
< BR />
< BR />
< ASP: requiredfieldvalidator ID = "Vupfile" Runat = "Server" Controltovalidate = "Upfiletest"
Errormessage = "Select the file to upload! " > </ ASP: requiredfieldvalidator > < BR />
< BR />
< BR />
< BR />
< ASP: button ID = "Btntext" Runat = "Server" Onclick = "Btntext_click" Text = "Upload and add a text watermark" />
& Nbsp;
< BR />
< BR />
< BR />
< BR />
< ASP: Label ID = "Lblstatus" Runat = "Server" > </ ASP: Label > </ Div >
</ Form >
</ Body >
</ Html >
Backend CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. drawing;
Using system. Drawing. imaging;
Using system. IO;
Add a watermark to a namespace Image
{
Public partial class watermarktest: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Lblstatus. Visible = false;
}
}
Private void show (string Str)
{
Response. Write (" < Script Language = 'Javascript' > Alert ( ' "+ STR +" ' ) </ Script > ");
}
Private void upload ()
{
String fullname = upfiletest. postedfile. filename;
String filename = fullname. substring (fullname. lastindexof ("\") + 1 );
String marks = fullname. substring (fullname. lastindexof (".") + 1); // image format
If (marks. tolower ()! = "Jpg" & marks. tolower ()! = "GIF ")
{
Show ("the format of the uploaded file is incorrect. Please reselect it! ");
}
Else
{
String upfilename = server. mappath ("uploadfiles") + "\" + system. datetime. Now. tostring ("yyyymmddhhmmss") + "." + marks;
Session ["filepath"] = upfilename;
Session ["marks"] = marks;
Upfiletest. postedfile. saveas (upfilename );
}
}
Protected void btntext_click (Object sender, eventargs E)
{
Upload ();
// Start adding a text watermark
System. Drawing. Image IMG = system. Drawing. image. fromfile (session ["filepath"]. tostring ());
Graphics G = graphics. fromimage (IMG );
G. drawimage (IMG, 0, 0, IMG. Width, IMG. Height );
Font F = new font ("", 10); // font model size
Brush B = new solidbrush (color. fromargb (70, color. whitesmoke); // the color of the paint brush
String STR = "watermark name ";
G. drawstring (STR, F, B, IMG. width/2, IMG. Height * 9/10); // watermark position
G. Dispose ();
String newfilepath = server. mappath ("uploadfiles") + "\" + system. datetime. Now. tostring ("yyyymmddhhmmss") + "."
+ Session ["marks"]. tostring ();
IMG. Save (newfilepath );
IMG. Dispose ();
If (file. exists (session ["filepath"]. tostring ()))
{
File. Delete (session ["filepath"]. tostring ());
}
Lblstatus. Visible = true;
Lblstatus. Text = "watermark drawn successfully! ";
}
}
}
In the editing process. AppearedA general error occurs in GDI +.. The reason may be that the bitmap object is not released after being created.
A Bitmap object or an image object is constructed from a file. The object remains locked for the lifetime of the object. Therefore, you cannot change the image and save it back to generate the same file.
Alternative Method
• Create a non-index image.
• Create an index image.
In both cases, the bitmap. Dispose () method is called on the original bitmap to delete the lock or delete the file. The stream or memory is active.
Create a non-indexed Image
Even if the original image is indexed in the format, This method requires that the new image be in a non-indexed pixel format that is located at each pixel (more than 8 bits. This work und uses the graphics. drawimage () method to copy an image to a new bitmap object:
1. Construct the original bitmap of the slave stream, slave memory, or slave file.
2. Create a New bitmap of the same size, with more than 8-pixel (BPP) per pixel format.
3. Use the graphics. fromimage () method to obtain the two-bitmap graphics object.
4. Used for graphics. drawimage () to draw the first bitmap to the second bitmap.
5. Used for graphics. Dispose () disposal is a graph.
6. bitmap. Dispose () is the first bitmap disposal.
Create index image
This solution creates a bitmap object in the index format:
1. Construct the original bitmap of the slave stream, slave memory, or slave file.
2. Create a New bitmap with the same size and pixel format as the first bitmap.
3. Use the bitmap. lockbits () method to lock the entire image. For two bitmap objects, use their native pixel format.
4. Use the marshal. Copy function or other memory copy function to copy the first bitmap to the second bitmap.
5. Use the bitmap. unlockbits () method to unlock two bitmap objects.
6. bitmap. Dispose () is the first bitmap disposal.
Because the foreign thinking is different from ours, I will use an example to explain how to create a non-indexed image here.
Private Void Toolstripmenuitem_click ( Object Sender , Eventargs E )
{
If ( Openfiledialog1 . Showdialog () = Dialogresult . OK )
{
// Create a bitmap type BMP variable to read the file.
Bitmap BMP = New Bitmap ( Openfiledialog1 . Filename );
// Create the second bitmap type BMP 2 variable. Here, based on myProgramWhich needs to be set.
Bitmap BMP 2 = New Bitmap ( 1024 , 768 , Pixelformat . Format16bpprgb555 );
// Copy the first BMP to BMP 2
Graphics Draw = Graphics . Fromimage ( BMP 2 );
Draw . Drawimage ( BMP , 0 , 0 );
Picturebox1 . Image = ( Image ) BMP 2 ; // Read BMP 2 to picturebox
File = Openfiledialog1 . Filename ;
Openfiledialog1 . Dispose ();
Draw . Dispose ();
BMP . Dispose (); // Release BMP file resources
}
}
After reading the file above, no error will occur during saving.
But this is not the problem. At last, we found that the problem actually exists. The image is saved in the same folder before and after the watermark is added, resulting in an error.
The solution is to create a folder for saving the watermark image. And:
Run the following code:
String newfilepath = server. mappath ("uploadfiles") + "\" + system. datetime. now. tostring ("yyyymmddhhmmss") + ". "+ session [" marks "]. tostring ();
Changed to: String newfilepath = server. mappath ("newmarkfiles") + "\" + system. datetime. now. tostring ("yyyymmddhhmmss") + ". "+ session [" marks "]. tostring ();
the name of the new folder is newmarkfiles