@@ -14,6 +14,8 @@ namespace Aspose.Slides.WebExtensions.Helpers
1414{
1515 public static class ImageHelper
1616 {
17+ public const int DefaultDpi = 72 ;
18+
1719 public static string GetImageURL < T > ( IPPImage image , TemplateContext < T > model )
1820 {
1921 string result = "" ;
@@ -279,24 +281,24 @@ public static string CreateSvgFilter(PictureFrame pictureFrame, string id)
279281 public static string ApplyDPI ( string imgSrc , TemplateContext < PictureFrame > model , PicturesCompression dpi )
280282 {
281283 const string b64prefix = "data:image/png;base64, " ;
282- float resolution = 72f ;
284+ int resolution = 72 ;
283285 switch ( dpi )
284286 {
285- case PicturesCompression . Dpi330 : resolution = 330f ; break ;
286- case PicturesCompression . Dpi220 : resolution = 220f ; break ;
287- case PicturesCompression . Dpi150 : resolution = 150f ; break ;
288- case PicturesCompression . Dpi96 : resolution = 96f ; break ;
289- case PicturesCompression . Dpi72 : resolution = 72f ; break ;
287+ case PicturesCompression . Dpi330 : resolution = 330 ; break ;
288+ case PicturesCompression . Dpi220 : resolution = 220 ; break ;
289+ case PicturesCompression . Dpi150 : resolution = 150 ; break ;
290+ case PicturesCompression . Dpi96 : resolution = 96 ; break ;
291+ case PicturesCompression . Dpi72 : resolution = 72 ; break ;
290292 default : return imgSrc ;
291293 }
292294 return imgSrc . StartsWith ( b64prefix )
293295 ? ApplyDPIEmbed ( imgSrc , model , resolution )
294296 : ApplyDPIFile ( imgSrc , model , resolution ) ;
295297 }
296298
297- private static string ApplyDPIFile ( string imgSrc , TemplateContext < PictureFrame > model , float resolution )
299+ private static string ApplyDPIFile ( string imgSrc , TemplateContext < PictureFrame > model , int resolution )
298300 {
299- using ( Bitmap bmpCompressed = GetImageCompressed ( model , resolution ) )
301+ using ( Image bmpCompressed = GetImageCompressed ( model , resolution ) )
300302 {
301303 var slidesPath = model . Global . Get < string > ( "slidesPath" ) ;
302304 string convertedFileName = GetImageURL ( model . Object . PictureFormat . Picture . Image , model ) . Replace ( ".png" , string . Format ( "red{0}.png" , model . Object . UniqueId ) ) ;
@@ -309,9 +311,9 @@ private static string ApplyDPIFile(string imgSrc, TemplateContext<PictureFrame>
309311 }
310312 }
311313
312- private static string ApplyDPIEmbed ( string imgSrc , TemplateContext < PictureFrame > model , float resolution )
314+ private static string ApplyDPIEmbed ( string imgSrc , TemplateContext < PictureFrame > model , int resolution )
313315 {
314- using ( Bitmap bmpCompressed = GetImageCompressed ( model , resolution ) )
316+ using ( Image bmpCompressed = GetImageCompressed ( model , resolution ) )
315317 {
316318 using ( MemoryStream buffer = new MemoryStream ( ) )
317319 {
@@ -322,34 +324,68 @@ private static string ApplyDPIEmbed(string imgSrc, TemplateContext<PictureFrame>
322324 }
323325 }
324326
325- private static Bitmap GetImageCompressed ( TemplateContext < PictureFrame > model , float resolution )
327+ private static Image GetImageCompressed ( TemplateContext < PictureFrame > model , int resolution )
326328 {
327329 PictureFrame pictureFrame = model . Object ;
328330 RectangleF boundRect = pictureFrame . Frame . Rectangle ;
329- float factor = resolution / 72f ;
330- int newWidth = ( int ) ( boundRect . Width * factor ) ;
331- int newHeight = ( int ) ( boundRect . Height * factor ) ;
332- PixelFormat pixFmt ;
333331 Image originImage = Image . FromStream ( new MemoryStream ( model . Object . PictureFormat . Picture . Image . BinaryData ) ) ;
334- if ( originImage . Width < newWidth || originImage . Height < newHeight )
335- return new Bitmap ( originImage ) ;
336- using ( Bitmap originalBmp = new Bitmap ( originImage ) )
337- pixFmt = originalBmp . PixelFormat ;
338- Bitmap newBmp = new Bitmap ( newWidth , newHeight , pixFmt ) ;
339- using ( Bitmap originalBmp = new Bitmap ( originImage ) )
332+ var newSize = GetCompressedSize ( originImage , resolution , boundRect . Size , boundRect ) ;
333+ return ImageCompress ( originImage , newSize , originImage . PixelFormat , new RectangleF ( 0 , 0 , originImage . Width , originImage . Height ) ) ;
334+ }
335+
336+
337+ public static SizeF GetCompressedSize ( Image sourceImage , int dpi , SizeF bounds , RectangleF rect )
338+ {
339+ SizeF compressedSize ;
340+
341+
342+ if ( dpi == DefaultDpi )
340343 {
341- newBmp . SetResolution ( resolution , resolution ) ;
342- using ( Graphics g = Graphics . FromImage ( newBmp ) )
343- {
344- g . PixelOffsetMode = PixelOffsetMode . HighQuality ;
345- g . SmoothingMode = SmoothingMode . AntiAlias ;
346- g . InterpolationMode = InterpolationMode . NearestNeighbor ;
344+ compressedSize = new SizeF ( rect . Width , rect . Height ) ;
345+ }
346+ else if ( dpi == 0 )
347+ {
348+ compressedSize = sourceImage . Size ;
349+ }
350+ else
351+ {
352+ compressedSize = ImageUtil_Convert ( dpi , bounds ) ;
353+ }
347354
348- g . DrawImage ( originalBmp , 0 , 0 , newWidth , newHeight ) ;
349- g . Flush ( ) ;
350- }
355+ if ( sourceImage . Width <= compressedSize . Width && sourceImage . Height <= compressedSize . Height )
356+ {
357+ compressedSize = sourceImage . Size ; // there're no sense to increase image according to DPI if source image DPI less than a target.
358+ }
359+
360+ return compressedSize ;
361+ }
362+
363+ private static SizeF ImageUtil_Convert ( int dpi , SizeF size )
364+ {
365+ if ( dpi < 0 )
366+ {
367+ throw new ArgumentException ( "dpi" ) ;
368+ }
369+
370+ if ( dpi == 0 || dpi == DefaultDpi )
371+ {
372+ return size ;
373+ }
374+
375+ float coeff = ( float ) dpi / 72f ;
376+
377+ return new SizeF ( size . Width * coeff , size . Height * coeff ) ;
378+ }
379+ public static Image ImageCompress ( Image sourceImage , SizeF compressedSize , PixelFormat pixelFormat , RectangleF originalSourceRect )
380+ {
381+ Image tempImage = new Bitmap ( ( int ) compressedSize . Width , ( int ) compressedSize . Height , pixelFormat ) ;
382+ using ( Graphics g = Graphics . FromImage ( tempImage ) )
383+ {
384+ RectangleF destRect = new RectangleF ( 0 , 0 , ( int ) compressedSize . Width , ( int ) compressedSize . Height ) ;
385+ g . InterpolationMode = InterpolationMode . HighQualityBicubic ;
386+ g . DrawImage ( sourceImage , destRect , originalSourceRect , GraphicsUnit . Pixel ) ;
351387 }
352- return newBmp ;
388+ return tempImage ;
353389 }
354390 }
355391}
0 commit comments