Guest User

Untitled

a guest
Jan 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. filenameb = "img-1b.jpg";//old img
  2. fullpathb = Path.Combine(dir, filenameb);//old img path
  3. //downloading using imglink
  4. client.DownloadFile(imglink, fullpathb);//save old img as %dir%img-1b.jpg
  5. //until here I downloaded the "old" img
  6.  
  7. filename = "img-1.jpg";//new img
  8. fullpath = Path.Combine(dir, filename);//new img path
  9. //name and fullpath for the "new img" are set
  10.  
  11. Image imgresize = Image.FromFile(fullpathb);//give old img path
  12. imgresize = FixedSize(imgresize);//resize
  13. imgresize.Save(fullpath, ImageFormat.Jpeg);//save new img as %dir%img-1.jpg
  14.  
  15. //EVERYTHING WORKS PERFECTLY UP TO HERE
  16.  
  17. imgresize.Dispose();//dispose -has old img path
  18. System.IO.File.Delete(fullpathb);//delete old img
  19.  
  20. //kind of messed up to save some space
  21. static Image FixedSize(Image imgPhoto)
  22. {
  23. int Width = 300;int Height = 250;
  24. int sourceWidth = imgPhoto.Width;int sourceHeight = imgPhoto.Height;
  25. int sourceX = 0;int sourceY = 0;int destX = 0;int destY = 0;
  26. float nPercent = 0;float nPercentW = 0;float nPercentH = 0;
  27.  
  28. nPercentW = ((float)Width / (float)sourceWidth);
  29. nPercentH = ((float)Height / (float)sourceHeight);
  30.  
  31. if (nPercentH < nPercentW){ nPercent = nPercentH;
  32. destX = (int)((Width - (sourceWidth * nPercent)) / 2);}
  33. else { nPercent = nPercentW;
  34. destY = (int)((Height - (sourceHeight * nPercent)) / 2); }
  35.  
  36. int destWidth = (int)(sourceWidth * nPercent);
  37. int destHeight = (int)(sourceHeight * nPercent);
  38.  
  39. Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  40. bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  41.  
  42. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  43. grPhoto.Clear(Color.White);
  44. grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
  45.  
  46. grPhoto.DrawImage(imgPhoto,
  47. new Rectangle(destX, destY, destWidth, destHeight),
  48. new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
  49. GraphicsUnit.Pixel);
  50.  
  51. grPhoto.Dispose();
  52. return bmPhoto;
  53. }
Add Comment
Please, Sign In to add comment