Two examples of c generating HD thumbnails are Shared

  • 2020-06-19 11:38:51
  • OfStack


/// <summary> 
 ///  Generate thumbnails for images    
 /// </summary> 
 /// <param name="phyPath"> The path of the original image </param> 
/// <param name="width"> The thumbnail wide </param> 
 /// <param name="height"> The thumbnail is high </param> 
 /// <returns></returns> 
public System.Drawing.Image GetThumbnail(System.Drawing.Image image, int width, intheight) 
{ 
Bitmap bmp = newBitmap(width, height); 
// from Bitmap create 1 a System.Drawing.Graphics 
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); 
// Set up the   
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
// This one down here is also going to be high quality  
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
// Let's set this down here High 
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
// Draws the original image as a scaled down version of the width and height set above  
System.Drawing.Rectangle rectDestination = newSystem.Drawing.Rectangle(0, 0, width, height); 
gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); 
returnbmp; 
}
 

A method is called


HttpPostedFile file = photoFile.PostedFile; 
if(!file.ContentType.Contains("image")) 
{ 
return" The photo format is illegal "; 
} 
stringext = Path.GetExtension(file.FileName).ToLower(); 
if (ext != ".jpg" && ext != ".gif" && ext != ".png"&& ext != ".jpeg") 
{ 
return" Could you please upload jpg , gif , png The picture "; 
} 
if(file.ContentLength > 5 * 1024 * 1024) 
{ 
return" Could you please upload 512 Image in bytes "; 
} 
stringnewName = Guid.NewGuid().ToString(); 
stringtempPath = "upload/"; 
stringimg = tempPath + newName + ext; 
stringfilePath = Server.MapPath(img); 
if(!Directory.Exists(tempPath)) 
{ 
Directory.CreateDirectory(tempPath); 
} 
using(System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream)) 
{ 
GetThumbnail(originalImage, 504, 374).Save(filePath); 

Example 2


public void  MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
// Get the original image   
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
// Thumbnail canvas width and height   
int towidth = width;
int toheight = height;
// Write the original image to the canvas coordinates and width and height ( Used to set the cut overflow portion )  
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
// Original picture canvas , Sets the write thumbnail canvas coordinates and width and height ( Used to scale the overall width and height of the original image )  
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
// Multiple variables   
double multiple = 0;
// Gets the width or height multiples of the thumbnail   
if (originalImage.Width >= originalImage.Height)
multiple = (double)originalImage.Width / (double)width;
else
multiple = (double)originalImage.Height / (double)height;
// The width and height of the uploaded image is equal to the thumbnail   
if (ow <= width && oh <= height)
{
// Thumbnail by original width and height   
bg_w = originalImage.Width;
bg_h = originalImage.Height;
// Fill in the blank with background color   
bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
}
// The width and height of the uploaded images are larger than the thumbnails   
else
{
// The width and height are scaled   
bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
// Fill in the blank with background color   
bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
}
// new 1 a bmp The picture , And set the thumbnail size .  
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
// new 1 A drawing board   
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
// Set up a high quality interpolation   
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
// Set high quality , Low speed presents smoothness   
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// Empty the canvas and set the background color   
g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
// Draws the specified part of the original image at the specified location and at the specified size   
// The first 1 a System.Drawing.Rectangle Is the canvas coordinates and width and height of the original image , The first 2 One is the coordinates and width and height of the original picture written on the canvas , The last 1 Parameters are specified in pixels   
g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
// Get image type   
string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
// Save the thumbnails according to the original image type , Images that do not follow the original format will appear blurred , Serrations and so on .  
switch (fileExtension)
{
case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break;
case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break;
case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}


Related articles: