How to use ASP Component AspJpeg to generate thumbnails

Source: Internet
Author: User

1. Add watermarks to images 

The Code is as follows: <%

Dim Jpeg ''' // declare the variable
Set Jpeg = Server. CreateObject ("Persits. Jpeg") ''' // call the component
Jpeg. Open Server. MapPath ("aaa. JPG") ''' // source Image Location
Jpeg. Canvas. Font. Color = & H000000 ''' // watermark Font Color
Jpeg. Canvas. Font. Family = "" ''' // watermark Font
Jpeg. Canvas. Font. Size = 14''' // watermark Font Size
Jpeg. Canvas. Font. Bold = False ''' // specifies whether the image is in Bold. Use True in Bold.
Jpeg. Canvas. Font. BkMode = & hffffff''' // Font background color
Jpeg. Canvas. Print 10, 10, "unbeaten urchin Studio" ''' // watermark text. Two digits 10 are the xy coordinates of the watermark.
Jpeg. Save Server. MapPath ("aaa_05.jpg") ''' // generate a new image with a watermark and its storage location
Set Jpeg = Nothing ''' // deregister the component and release the resource.
Response. Write "%>

ASP image watermark AspJpeg v1.8 Special Edition
2. Generating thumbnails 

The Code is as follows: <%
Dim Jpeg ''' // declare the variable
Set Jpeg = Server. CreateObject ("Persits. Jpeg") ''' // call the component
Jpeg. Open Server. MapPath ("aaa. JPG") ''' // source Image Position
Jpeg. Width = Jpeg. OriginalWidth/4 ''' // set the image Width to 1/4 of the source image Width.
Jpeg. Height = Jpeg. OriginalHeight/4 ''' // set the Image Height to 1/4 of the source Image Height.
Jpeg. Sharpen 1,130 ''' // sets the sharpening effect.
Jpeg. Save Server. MapPath ("aaa_small.jpg") ''' // generates the thumbnail position and name.
Set Jpeg = Nothing ''' // deregister the component and release the resource.
Response. Write "%>

Advanced usage of the aspjpeg component
Aspjpeg is a powerful image processing component in English only. However, there are already free and cracked versions, but there are not many articles about them in detail and in depth, even if they only involve image scaling and image watermarks. It may be because it is only English.
Here I will talk about the advanced usage of aspjpeg to address these issues. The technologies here mainly include:
Thumbnail
Image Watermark
Security code technology
Image Cutting
Image Merging
Database Support
More uncommon Methods
And some related practical technologies
The only disadvantage of aspjpeg is that the output mode is relatively simple. Here, we mainly talk about the output method that is called after image processing is saved. In addition, I am relatively lazy, so some code is still referenced in the original document. I will explain it even if I don't understand it!
Comrades who have studied vb or. net must have understood it at a glance. Brush. Haha.
1. scaling down images 

The Code is as follows: <%
Set Jpeg = Server. CreateObject ("Persits. Jpeg") Call the component
Path = Server. MapPath ("images") & "\ clock.jpg" Path of the image to be processed
Jpeg. Open Path
The height and width are 1/2 of the original image.
Jpeg. Width = Jpeg. OriginalWidth/2
Jpeg. Height = Jpeg. OriginalHeight/2
Save image
Jpeg. Save Server. MapPath ("images") & "\ clock_small.jpg"
%>

View the processed image
2. Image Watermark 

The Code is as follows: <%
Set Jpeg = Server. CreateObject ("Persits. Jpeg ")
Jpeg. Open Server. MapPath ("images/dodge_viper.jpg ")
Start writing text
Jpeg. Canvas. Font. Color = & h000000''' white Color
Jpeg. Canvas. Font. Family = "Courier New" Font
Jpeg. Canvas. Font. Bold = True Bold or not
Jpeg. Canvas. Print 10, 10, "Copyright (c) XYZ, Inc ."
Print coordinates x print coordinates y print characters
The following is the border processing for the image
Jpeg. Canvas. Pen. Color = & h000000''' black Color
Jpeg. Canvas. Pen. Width = 2 paint Width
Jpeg. Canvas. Brush. Solid = False whether to perform bold Processing
Jpeg. Canvas. Bar 1, 1, Jpeg. Width, Jpeg. Height
Start X coordinate start Y coordinate input length input height
Jpeg. Save Server. MapPath ("images/dodge_viper_framed.jpg") Save
%>

3. Security Code
The principle of security Ma is similar to watermarking. Many friends asked me to ask for specific code technologies. Here I will share it with you. I will not tell anyone else. Haha.Copy codeThe Code is as follows: <%
Function for generating security codes: www.wuyouw.com
Function make_randomize (max_len, w_n) max_len generation length. w_n: 0 may contain letters. 1: only numbers.
Randomize
For intcounter = 1 to max_len
Whatnext = int (1-0 + 1) * rnd + w_n)
If whatnext = 0 then
Upper = 122
Lower = 97
Else
Upper = 57
Lower = 48
End if
Strnewpass = strnewpass & chr (int (upper-lower + 1) * rnd) + lower)
Next
Make_randomize = strnewpass
End function
%>

Generate a picture of the security code. Of course, you need to prepare a background image in advance.

The Code is as follows: <% random_num = make_randomize () generates a four-digit security code
Session ("random_num") = Why does random_num call the session? the security code without the session is meaningless. Haha
Set Jpeg = Server. CreateObject ("Persits. Jpeg") Call the component
Jpeg. Open Server. MapPath ("infos/random_pic/random_index.gif") Open the prepared Image
Jpeg. Canvas. Font. Color = & H006699
Jpeg. Canvas. Font. Family = "Arial Black"
Jpeg. Canvas. Font. Bold = false
Jpeg. Canvas. PrintText 0,-2, random_num
Jpeg. save Server. MapPath ("infos/random_pic/random_index.bmp") save
%>


Do it yourself. Haha.
4. Image Cutting
For a long time, people who do not know about aspjpeg think that they cannot use it for cutting.
In fact, there is such a method
Crop x1, y1, x2, y2
Returns the x coordinate in the upper left corner of the rectangular shape, and the y coordinate in the lower right corner of the y coordinate.
Here is a demonstration.
Set Jpeg = Server. CreateObject ("Persits. Jpeg ")
Jpeg. open server. MapPath ("/pic/1.gif ")
Jpeg. width = 70
Jpeg. Height = Jpeg. OriginalHeight * 70/jpeg. Originawidth
Jpeg. crop, start to cut is actually to remove the lower part of more than 52 pixels
Jpeg. save server. MapPath ("/temp_pic/small_1.gif") save
How is it? It's easy.

5. Image Merging
We need to add the logoimage to the dodge_viper.jpg image.
In fact, the image merging method can also be used for dynamic watermarking.

The Code is as follows: Set Photo = Server. CreateObject ("Persits. Jpeg ")
PhotoPath = Server. MapPath ("images") & "\ dodge_viper.jpg"
Photo. Open PhotoPath
Set Logo = Server. CreateObject ("Persits. Jpeg ")
LogoPath = Server. MapPath ("images") & "\ clock.jpg"
Logo. Open LogoPath
Logo. Width = 70
Logo. Height = Logo. Width * Logo. OriginalHeight/Logo. OriginalWidth
Photo. DrawImage 0, 0, Logo
Photo. SendBinary

The output method of sendBinary is used here. Of course, you can save and modify the dodge_viper.jpg file, and then enter it. I personally do not like to use the sendBinary method, which is prone to errors when the network speed is slow. The speed is not very good. Haha.
6. Database Support
I will not talk about it here. It is actually the Binary method. We know that images can only be saved as Binary files in the database. So the code is too lazy to write.

7. More Methods
Canvas. Line (Left, Top, Right, Bottom)
Draw a straight line
Canvas. Ellipse (Left, Top, Right, Bottom)
Draw an elliptic
Canvas. Circle (X, Y, Radius)
Draw a circle
Canvas. Bar (Left, Top, Right, Bottom)
Draw a rectangle with code on it.
Canvas. Font. ShadowColor
Text shadow color
Canvas. Font. ShadowXOffset As Long
Shadow X coordinate settings
Canvas. Font. ShadowYOffset As Long
Y coordinate settings
Canvas. Font. BkMode As String
Text background
==========================================================
Today, I want to talk about ASP's knowledge of watermarking images.
Adding watermarks to Images Using ASP requires components... common examples include aspjpeg and wsImage developed by the Chinese... the former has 30 days of free... the latter is free of charge... of course we will use Chinese products .. hey hey ..
How to register Components:
Enter "regsvr32 [Dll path]" at the command prompt.
Adding a watermark to an image is nothing more than obtaining the image size, and then writing the watermark. ASP code is just a control component. Use code to describe everything.

1. Obtain the image size(This is represented by pixel values. All the friends who learn PhotoShop should understand)

The Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize") ''' call the component
Obj. LoadSoucePic server. mappath ("25.jpg") ''''''open the image. The image name is 25.jpg.
Obj. GetSourceInfo iWidth, iHeight
Response. write "Image Width:" & iWidth & "<br>" ''' get the image width.
Response. write "Image Height:" & iHeight & "<br>" ''' get the Image Height
StrError = obj. errorinfo
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

2. Add a text watermark 

the Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg") '''
Obj. Quality = 75
Obj. TxtMarkFont = "文" ''' sets the watermark text font.
Obj. TxtMarkBond = false ''' set the watermark text width.
Obj. MarkRotate = 0 ''' watermark text Rotation Angle
Obj. TxtMarkHeight = 25 ''' height of the watermark text
Obj. AddTxtMark server. mappath ("txtMark.jpg"), "Take you away", & H00FF00 &, 10, 70
StrError = obj. errorinfo ''' to generate the image name. The text color is the position of the watermark in the image.
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

3. Add an image watermark

The Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg") '''
Obj. LoadImgMarkPic server. mappath ("blend.bmp") ''' load the watermark image
Obj. Quality = 75
Obj. AddImgMark server. mappath ("imgMark.jpg"), 315,220, & hFFFFFF, 70
StrError = obj. errorinfo ''' to generate the image name. The text color is the position of the watermark in the image.
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

In fact, adding a watermark to an image is that simple. Then I will explain the other two main usage of the WsImage. dll component, including:
Crop an image to generate a thumbnail.

I still get used to it and add comments with the code to describe:
Crop Images:

The Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg ")
Obj. Quality = 75
Obj. cropImage server. mappath ("25_crop.jpg"), 200,200, ''' defines the cut size and name of the generated image.
StrError = obj. errorinfo
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

Note: The CropImage method of WsImage is used for image cropping. when an image is generated, 100 and 10 are the cut points in the upper left corner, that is, pixels on the left and 10 pixels on the top. the last two 200 represents the cut bandwidth and height.
Generate image thumbnails:Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg") ''' load the image
Obj. Quality = 75
Obj. OutputSpic server. mappath ("25_s.jpg"), 0.5, 0.5, 3 ''' defines the size of the thumbnail.
StrError = obj. errorinfo
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

Detailed description:
There are four export methods for generating thumbnails:
(1) obj. OutputSpic server. mappath ("25_s.jpg"), 200,150, 0
200 is the output width and 150 is the output height. This output form is forced output width and height, which may cause image deformation.
(2) obj. OutputSpic server. mappath ("25_s.jpg"), 200,0, 1
The output width is 200, and the output height scales with the ratio column.
(3) obj. OutputSpic server. mappath ("25_s.jpg"), 0,200, 2
The output height is 200, and the output width scales with the ratio column.
(4) obj. OutputSpic server. mappath ("25_s.jpg"), 0.5, 0.5, 3
The first 0.5 indicates that the generated thumbnail is half the width of the source image, that is, the width reduction ratio.
The second 0.5 indicates that the generated thumbnail is half the height of the source image, that is, the scaled down ratio is high.
The scaled-down ratio of the width and height is the same, which means that the ratio of the source image is reduced. If the zoom ratio is greater than 1, the source image is enlarged.
From: http://hi.baidu.com/miracle521/blog/item/e3419133fdc00746ac4b5f25.html
2 cores -----------------------------------------------------------------------------------
Asp.net uploads an image watermark (Text watermark, image watermark, text + image watermark)
Adding watermarks to images (Text watermarks, image watermarks, text + image watermarks)
:

Figure 500) this. width = 500 "border = 0 <

Watermark 500) this. width = 500 "border = 0 <

Add a watermark to the image (pay attention to the upper right corner + at the bottom)
500) this. width = 500 "border = 0 <

Code:
DrawImg. csCopy codeThe Code is as follows: using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Drawing2D;
Public class DrawImg
{
Private string WorkingDirectory = string. Empty; // path
Private string ImageName = string. Empty; // the image to be processed.
Private string ImageWater = string. Empty; // watermark image
Private string FontString = string. Empty; // watermark text

Enum DealType {NONE, WaterImage, WaterFont, DoubleDo}; // enumeration command
Private DealType dealtype;

Public DrawImg ()
{}
Public string PublicWorkingDirectory
{
Get
{
Return WorkingDirectory;
}
Set
{
WorkingDirectory = value;
}
}
Public string PublicImageName
{
Get
{
Return ImageName;
}
Set
{
ImageName = value;
}
}

Public string PublicImageWater
{
Get
{
Return ImageWater;
}
Set // If a watermark image is set, the watermark image effect is required.
{
Dealtype = DealType. WaterImage;
ImageWater = value;
}
}
Public string PublicFontString
{
Get
{
Return FontString;
}
Set // If the watermark text is set, the watermark text effect is required.
{
Dealtype = DealType. WaterFont;
FontString = value;
}
}

Public void DealImage ()
{
IsDouble ();
Switch (dealtype)
{
Case DealType. WaterFont: WriteFont (); break;
Case DealType. WaterImage: WriteImg (); break;
Case DealType. DoubleDo: WriteFontAndImg (); break;
}
}
Private void IsDouble ()
{
If (ImageWater + ""! = "" & FontString + ""! = "")
{
Dealtype = DealType. DoubleDo;
}
}
Private void WriteFont ()
{
// Set a working directory
// String WorkingDirectory = @ "C: \ Watermark_src \ WaterPic ";
// Define a string of text to use as the Copyright message
// String Copyright = "Copyright? 2002-AP Photo/David zaluboski ";
// Create a image object containing the photograph to watermark
Image imgPhoto = Image. FromFile (WorkingDirectory + ImageName );
Int phWidth = imgPhoto. Width;
Int phHeight = imgPhoto. Height;
// Create a Bitmap the Size of the original photograph
Bitmap BMP hoto = new Bitmap (phWidth, phHeight, PixelFormat. Format24bppRgb );
BMP hoto. SetResolution (imgPhoto. HorizontalResolution, imgPhoto. VerticalResolution );
// Load the Bitmap into a Graphics object
Graphics grPhoto = Graphics. FromImage (BMP hoto );
//------------------------------------------------------------
// Step #1-Insert Copyright message
//------------------------------------------------------------
// Set the rendering quality for this Graphics object
GrPhoto. SmoothingMode = SmoothingMode. AntiAlias;
// Draws the photo Image object at original size to the graphics object.
GrPhoto. DrawImage (
ImgPhoto, // Photo Image object
New 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 wowould 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], FontStyle. Bold );
CrFont = new Font ("arial", sizes [I], FontStyle. Bold );
// Measure the Copyright string in this Font
CrSize = grPhoto. MeasureString (FontString, crFont );
If (ushort) crSize. Width <(ushort) phWidth)
Break;
}
// Since all photographs will have varying heights, determine
// 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. Center;
// Define a Brush which is semi trasparent black (Alpha set to 153)
SolidBrush semiTransBrush2 = new SolidBrush (Color. FromArgb (153, 0, 0, 0 ));
// Draw the Copyright string
GrPhoto. DrawString (FontString, // string of text
CrFont, // font
SemiTransBrush2, // Brush
New PointF (xCenterOfImg + 1, yPosFromBottom + 1), // Position
StrFormat );
// Define a Brush which is semi trasparent white (Alpha set to 153)
SolidBrush semiTransBrush = new SolidBrush (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
GrPhoto. DrawString (FontString, // string of text
CrFont, // font
SemiTransBrush, // Brush
New PointF (xCenterOfImg, yPosFromBottom), // Position
StrFormat );
ImgPhoto = BMP hoto;
GrPhoto. Dispose ();
// Save new image to file system.
ImgPhoto. Save (WorkingDirectory + ImageName + "_finally.jpg", ImageFormat. Jpeg );
ImgPhoto. Dispose ();
// Text alignment
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.