File Size of Aspose.Word Documents containing images

Hi,

2 documents are attached to this post. The first one hase been generated with aspose.word, the second is the same as the first but I inserted manually the image in chapter 13 from msword.
Please, could you tell me how to decrease file size generated by aspose.word.

I use this methode to insert image file, but the result is not acceptable :

Sub HandleMergeImageField(ByVal sender As Object, ByVal e As Aspose.Words.Reporting.MergeImageFieldEventArgs)

If e.FieldValue Is Convert.DBNull OrElse e.FieldValue = Nothing Then Exit Sub
If e.FieldName.Contains("ImgCertificat") Then
Dim a As String = ""
End If
Dim str() As String = Split(e.FieldName, " ")
Dim lg As Integer = 0 ’ Largeur maximale de l’image dans le rapport
Dim ht As String = 0 ’ Hauteur maximale de l’image dans le rapport

Dim Fic As String = ""
Dim spots As String = ""

mBuilder.MoveToMergeField(e.FieldName)
If str.Length > 2 Then ’ les largeurs et hauteurs sont passées
If IsNumeric(str(1)) Then lg = CInt(str(1))
If IsNumeric(str(2)) Then ht = CInt(str(2))
Else
’ On prend la largeur de la page
Dim a As Aspose.Words.Section = mBuilder.CurrentSection
lg = (a.PageSetup.PageWidth - a.PageSetup.RightMargin - a.PageSetup.LeftMargin)
ht = (a.PageSetup.PageHeight - a.PageSetup.TopMargin - a.PageSetup.BottomMargin)
End If
Dim st2() As String = Split(CStr(e.FieldValue), Chr(0))
Fic = st2(0).Trim

If Fic = "" Then Exit Sub
Dim fi As New IO.FileInfo(Fic)
If Not fi.Exists Then Exit Sub

Dim img As System.Drawing.Bitmap = New System.Drawing.Bitmap(Fic)
Threading.Thread.Sleep(200)
If st2.Length > 1 Then
spots = st2(1)
img = FormatSpots(img, spots)
End If

’ Reformater l’image pour tenir dans les hauteurs et largeurs

Dim sc1 As Single = 1
Dim sc2 As Single = 1
If lg = 0 Then lg = img.Width
If ht = 0 Then ht = img.Height

If lg < img.Width Then
sc1 = lg / img.Width
End If

If ht < img.Height Then
sc2 = ht / img.Height
End If

Dim sc As Single = Math.Min(sc1, sc2)

'imgc = New System.Drawing.Bitmap(img, img.Width * sc, img.Height * sc) ’ Image contenant la photo

'img.Dispose()
’ Règles : 
’ Conserver l’image telle quelle et l’insérer à la taille de la zone d’impression
’ Adapter l’image à la taille de la zone d’impression
'If Not e.FieldName.ToLower.Contains("photo") Then
’ pas de réduction
'Dim img1 As System.Drawing.Bitmap = New System.Drawing.Bitmap(img, img.Width, img.Height)
'Dim sh As Aspose.Words.Drawing.Shape = mBuilder.InsertImage(img1, img1.Width * sc, img1.Height * sc)
'Else
‘’ Réduction

If sc > 1 Then
sc = 1
End If
Dim img1 As System.Drawing.Bitmap
img1 = New System.Drawing.Bitmap(CInt(img.Width * sc * 1.33 * 2), CInt(img.Height * sc * 1.33 * 2))
img1.SetResolution(96, 96)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(img1)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(img, New System.Drawing.Rectangle(0, 0, img1.Width, img1.Height), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel)
g.Dispose()
Dim sh As Aspose.Words.Drawing.Shape = mBuilder.InsertImage(img1, img1.Width / 1.33 / 2, img1.Height / 1.33 / 2)
'Dim sh As Aspose.Words.Drawing.Shape = mBuilder.InsertImage(img1, img1.Width * sc, img1.Height * sc)
e.Image = Nothing

End Sub

Thanks you

Regards

Hi
Thanks for your inquiry. Could you please also attach your image for testing? Most probably this occurs because you are changed resolution of image. I will investigate the problem and provide you more information.
Best regards.

Thanks Alexey,

Here is the image

Regards

Hi
Thank you for additional information. As o told you earlier this occurs because you change your image in code. Try using the following code for inserting image:

Dim img As System.Drawing.Bitmap = New System.Drawing.Bitmap(Fic)
Dim sh As Aspose.Words.Drawing.Shape = mBuilder.InsertImage(img)
If you need to resize image in your code you can try using the following code:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
''' 
''' Resize image
''' 
''' Source image
''' Target width
''' Target height
''' Resized image
''' 
Function ResizeImage(ByVal sourceImage As Image, ByVal targetWidth As Integer, ByVal targetHeight As Integer) As Image
Dim ratioWidth As Double = CType(sourceImage.Width, Double) / targetWidth
Dim ratioHeight As Double = CType(sourceImage.Height, Double) / targetHeight
If (ratioWidth > ratioHeight) Then
targetHeight = CType((targetHeight * (ratioHeight / ratioWidth)), Integer)
Else
If (ratioWidth < ratioHeight) Then
targetWidth = CType((targetWidth * (ratioWidth / ratioHeight)), Integer)
End If
End If
'create target image
Dim targetImage As Bitmap = New Bitmap(targetWidth, targetHeight, PixelFormat.Format24bppRgb)
targetImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution)
'set transform parameters 
Dim g As Graphics = Graphics.FromImage(targetImage)
Using (g)
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
'resize image
Dim rc As Rectangle = New Rectangle(0, 0, targetImage.Width, targetImage.Height)
g.DrawImage(sourceImage, rc)
End Using
Return CType(targetImage, Image)
End Function

Hope this helps
Best regards.