@@ -195,26 +195,41 @@ public override void GxDrawBitMap(string bitmap, int left, int top, int right, i
195195 {
196196 try
197197 {
198- string imageType = Path . GetExtension ( bitmap ) . Substring ( 1 ) ;
198+ string imageType = Path . GetExtension ( bitmap ) . Substring ( 1 ) . ToLowerInvariant ( ) ;
199199
200200 float rightAux = ( float ) convertScale ( right ) ;
201201 float bottomAux = ( float ) convertScale ( bottom ) ;
202202 float leftAux = ( float ) convertScale ( left ) ;
203203 float topAux = ( float ) convertScale ( top ) ;
204-
204+
205205 float llx = leftAux + leftMargin ;
206- float lly = ( float ) pageBuilder . PageSize . TopRight . Y - bottomAux - topMargin - bottomMargin ;
206+ float lly = ( float ) pageBuilder . PageSize . TopRight . Y - bottomAux - topMargin - bottomMargin ;
207+ float boxWidth = rightAux - leftAux ;
208+ float boxHeight = bottomAux - topAux ;
209+
210+ byte [ ] imageBytes = null ;
211+ try
212+ {
213+ imageBytes = LoadImageBytes ( ref bitmap ) ;
214+ }
215+ catch ( Exception ) { }
216+
207217 float width ;
208218 float height ;
209- if ( aspectRatio == 0 )
219+ if ( aspectRatio == 0 || imageBytes == null )
210220 {
211- width = rightAux - leftAux ;
212- height = bottomAux - topAux ;
221+ width = boxWidth ;
222+ height = boxHeight ;
213223 }
214224 else
215225 {
216- width = ( rightAux - leftAux ) * aspectRatio ;
217- height = ( bottomAux - topAux ) * aspectRatio ;
226+ using ( var ms = new MemoryStream ( imageBytes ) )
227+ using ( var bmp = new Bitmap ( ms ) )
228+ {
229+ float scale = Math . Min ( boxWidth / bmp . Width , boxHeight / bmp . Height ) ;
230+ width = bmp . Width * scale ;
231+ height = bmp . Height * scale ;
232+ }
218233 }
219234
220235 PdfRectangle position = new PdfRectangle ( llx , lly , llx + width , lly + height ) ;
@@ -229,40 +244,18 @@ public override void GxDrawBitMap(string bitmap, int left, int top, int right, i
229244 {
230245 try
231246 {
232- byte [ ] imageBytes ;
233- if ( ! Path . IsPathRooted ( bitmap ) )
247+ if ( imageBytes != null )
234248 {
235- if ( PathUtil . IsAbsoluteUrl ( bitmap ) )
236- {
237- imageBytes = HttpHelper . DownloadFile ( bitmap , out _ ) ;
238- }
239- else
240- {
241- string bitmapPath = Path . Combine ( defaultRelativePrepend , bitmap ) ;
242- if ( File . Exists ( bitmapPath ) )
243- {
244- imageBytes = File . ReadAllBytes ( bitmapPath ) ;
245- bitmap = bitmapPath ;
246- }
247- else
248- {
249- bitmapPath = Path . Combine ( webAppDir , bitmap ) ;
250- imageBytes = File . ReadAllBytes ( bitmapPath ) ;
251- bitmap = bitmapPath ;
252- }
253- }
249+ bool isJpeg = imageType == "jpeg" || imageType == "jpg" ;
250+ image = isJpeg ? pageBuilder . AddJpeg ( imageBytes , position ) : pageBuilder . AddPng ( imageBytes , position ) ;
254251 }
255- else
252+ else if ( PathUtil . IsAbsoluteUrl ( bitmap ) )
256253 {
257- imageBytes = File . ReadAllBytes ( bitmap ) ;
254+ image = AddImageFromURL ( bitmap , position ) ;
258255 }
259- image = imageType == "jpeg" ? pageBuilder . AddJpeg ( imageBytes , position ) : pageBuilder . AddPng ( imageBytes , position ) ;
260- }
261- catch ( Exception )
262- {
263- image = AddImageFromURL ( bitmap , position ) ;
264256 }
265- if ( image == null )
257+ catch ( Exception ) { }
258+ if ( image == null && PathUtil . IsAbsoluteUrl ( bitmap ) )
266259 {
267260 image = AddImageFromURL ( bitmap , position ) ;
268261 }
@@ -300,6 +293,36 @@ private AddedImage AddImageFromURL(string url, PdfRectangle position)
300293 return image ;
301294 }
302295
296+ private byte [ ] LoadImageBytes ( ref string bitmap )
297+ {
298+ if ( ! Path . IsPathRooted ( bitmap ) )
299+ {
300+ if ( PathUtil . IsAbsoluteUrl ( bitmap ) )
301+ {
302+ return HttpHelper . DownloadFile ( bitmap , out _ ) ;
303+ }
304+ else
305+ {
306+ string bitmapPath = Path . Combine ( defaultRelativePrepend , bitmap ) ;
307+ if ( File . Exists ( bitmapPath ) )
308+ {
309+ bitmap = bitmapPath ;
310+ return File . ReadAllBytes ( bitmapPath ) ;
311+ }
312+ else
313+ {
314+ bitmapPath = Path . Combine ( webAppDir , bitmap ) ;
315+ bitmap = bitmapPath ;
316+ return File . ReadAllBytes ( bitmapPath ) ;
317+ }
318+ }
319+ }
320+ else
321+ {
322+ return File . ReadAllBytes ( bitmap ) ;
323+ }
324+ }
325+
303326 public override void GxAttris ( string fontName , int fontSize , bool fontBold , bool fontItalic , bool fontUnderline , bool fontStrikethru , int pen , int foreRed , int foreGreen , int foreBlue , int backMode , int backRed , int backGreen , int backBlue )
304327 {
305328 bool isCJK = false ;
0 commit comments