@@ -14,6 +14,8 @@ namespace Aspose.Slides.WebExtensions.Helpers
14
14
{
15
15
public static class ImageHelper
16
16
{
17
+ public const int DefaultDpi = 72 ;
18
+
17
19
public static string GetImageURL < T > ( IPPImage image , TemplateContext < T > model )
18
20
{
19
21
string result = "" ;
@@ -279,24 +281,24 @@ public static string CreateSvgFilter(PictureFrame pictureFrame, string id)
279
281
public static string ApplyDPI ( string imgSrc , TemplateContext < PictureFrame > model , PicturesCompression dpi )
280
282
{
281
283
const string b64prefix = "data:image/png;base64, " ;
282
- float resolution = 72f ;
284
+ int resolution = 72 ;
283
285
switch ( dpi )
284
286
{
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 ;
290
292
default : return imgSrc ;
291
293
}
292
294
return imgSrc . StartsWith ( b64prefix )
293
295
? ApplyDPIEmbed ( imgSrc , model , resolution )
294
296
: ApplyDPIFile ( imgSrc , model , resolution ) ;
295
297
}
296
298
297
- private static string ApplyDPIFile ( string imgSrc , TemplateContext < PictureFrame > model , float resolution )
299
+ private static string ApplyDPIFile ( string imgSrc , TemplateContext < PictureFrame > model , int resolution )
298
300
{
299
- using ( Bitmap bmpCompressed = GetImageCompressed ( model , resolution ) )
301
+ using ( Image bmpCompressed = GetImageCompressed ( model , resolution ) )
300
302
{
301
303
var slidesPath = model . Global . Get < string > ( "slidesPath" ) ;
302
304
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>
309
311
}
310
312
}
311
313
312
- private static string ApplyDPIEmbed ( string imgSrc , TemplateContext < PictureFrame > model , float resolution )
314
+ private static string ApplyDPIEmbed ( string imgSrc , TemplateContext < PictureFrame > model , int resolution )
313
315
{
314
- using ( Bitmap bmpCompressed = GetImageCompressed ( model , resolution ) )
316
+ using ( Image bmpCompressed = GetImageCompressed ( model , resolution ) )
315
317
{
316
318
using ( MemoryStream buffer = new MemoryStream ( ) )
317
319
{
@@ -322,34 +324,68 @@ private static string ApplyDPIEmbed(string imgSrc, TemplateContext<PictureFrame>
322
324
}
323
325
}
324
326
325
- private static Bitmap GetImageCompressed ( TemplateContext < PictureFrame > model , float resolution )
327
+ private static Image GetImageCompressed ( TemplateContext < PictureFrame > model , int resolution )
326
328
{
327
329
PictureFrame pictureFrame = model . Object ;
328
330
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 ;
333
331
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 )
340
343
{
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
+ }
347
354
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 ) ;
351
387
}
352
- return newBmp ;
388
+ return tempImage ;
353
389
}
354
390
}
355
391
}
0 commit comments