When an image is uploaded, SharePoint automatically generates a thumbnail (of course, the image format is required, but this is ignored first ). When we want to reference these thumbnails when writing our own applications, we want to use the object model to get the link of the thumbnail, which is not found for half a day. Recently, the company held a photography exhibition that used image libraries to collect works and then used thumbnails to create a voting application page. There is no way to solve this problem by directly referencing the thumbnail link address.
Exploring thumbnail rules:
- By using the image library process, we can know that when an image is uploaded, two types of thumbnails are automatically generated. One is a small image, and the other is a large image by default, used to view image library project details.
- The url is as follows: http: // site url/sub-site name/list name/_ t/xxx_JPG.jpg
- _ T is the path of all small images, and the big image is _ w
- Xxx_JPG is the source image file name. Replace "." with "_", and the suffix is .jpg.
After you know the above rules, start coding the link to get the thumbnail. Please refer to the following code:
Code
/// <Summary>
/// Title image size
/// </Summary>
Public enum TitileImageSize
{
Small,
Big
}
/// <Summary>
/// Image help class
/// </Summary>
Public static class ImageHelper
{
/// <Summary>
/// Obtain the image thumbnail URL
/// </Summary>
/// <Param name = "webUrl"> Site path (for example, SPContext. Current. Site. Url) </param>
/// <Param name = "sourceImageUrl"> source image path (relative site path, for example, "xxx/xxx.jpg") </param>
/// <Returns> thumbnail URL </returns>
Public static string GetTitleImageUrl (string webUrl, string sourceImageUrl, tititileimagesize size)
{
String result = string. Empty;
String temp = string. Empty;
// Replace the last "/" with "/_ t/". The replacement logic is implemented in the ReplaceLastGang method.
// If this step is not completed, the image library will leave the plane when it is in the sublevel site.
Regex myRegex = new Regex ("/");
MatchEvaluator myMatchEvaluator = new MatchEvaluator (ReplaceLastGang );
Temp = myRegex. Replace (sourceImageUrl, myMatchEvaluator );
Temp = temp. Replace (".","_");
Switch (size)
{
Case TitileImageSize. Small:
// Temp = "/_ t /";
Break;
Case TitileImageSize. Big:
Temp = temp. Replace ("/_ t/", "/_ w /");
Break;
Default:
Break;
}
Result = String. Format (@ "{0}/1_1).jpg", webUrl, temp );
Return result;
}
Private static string ReplaceLastGang (Match m)
{
If (m. NextMatch (). Index = 0)
{
Return "/_ t /";
}
Else
{
Return m. Value;
}
}
This code is already in the second version. In the first version, there is only one line of key code.
Result = String. format (@ "{0}/1_12.16.jpg", webUrl, sourceImageUrl. replace (". ","_"). replace ("/", sizeText); // sizeText is _ t or _ w
However, it is found that when the image is not on the Root website, but on the secondary or third-level sub-website, the problem may occur. Then, the last/is found using regular expressions before replacement.
If any brother or sister has a better method or knows which member of the object model is available, please send a message at http://ryu666.cnblogs.com/to us.
Finally, on the voting page, the processed items are for appreciation only.