c generates thumbnails without losing real method instances to share

  • 2020-10-23 20:56:23
  • OfStack


/// <summary>
///  Obtain micrographs 
/// </summary>
/// <returns></returns>
  public bool GetThumbImg()
{
try
{
string imgpath; // The original path 
     if(imgsourceurl.IndexOf("\",0)<0) // Relative paths are used 
     {
imgpath = HttpContext.Current.Server.MapPath(imgsourceurl); // Convert to physical path 
     }
else
{
imgpath=imgsourceurl;
}
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(imgpath);
int width = sourceImage.Width;
int height = sourceImage.Height;
if(thumbwidth <= 0)
{
thumbwidth = 120;
}
if(thumbwidth >= width)
{
return false;
}
else
{
(thumbwidth,thHeight*thumbwidth/thWidth,null,IntPtr.Zero);
Image imgThumb=new System.Drawing.Bitmap(thumbwidth,height*thumbwidth/width);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, thumbwidth,height*thumbwidth/width), 0, 0, width, height, GraphicsUnit.Pixel);
string thumbpath="";
sourceImage.Dispose();
if(thumburl=="")
{
thumbpath=imgpath;
}
if(thumbpath.IndexOf("\",0)<0)// Relative paths are used 
      {
thumbpath=HttpContext.Current.Server.MapPath(thumburl);// Convert to physical path 
      }
imgThumb.Save(thumbpath,ImageFormat.Jpeg);
imgThumb.Dispose();
return true;
}
}
catch
{
throw;
}
}


Related articles: