Home Image-Pro Automation (Macros, Apps, Reports)

Is it possible to adjust the image quality in the publication?

Hello everyone,
I would like to adjust the image quality(*.jpg) of a publication in a macro.
Could you please advise on the methods to achieve this?

Dim doc1 As IMcDocument = ThisApplication.ActiveDocument
Dim fName As String = doc1.FileName       
Dim outPath As String = ThisApplication.Settings("QuickSave", "Publication").Get("Path", ThisApplication.Path(mcPathType.mcptWritableImages))

'generate mask name, save it to the same folder as the original image, but with _Mask suffix in JPG format
Dim maskName As String = outPath & System.IO.Path.GetFileNameWithoutExtension(fName) & "_Mask" & ".jpg"

Dim varList1 = New List(1)

With MediaCy.Automation.Application.WindowCommands.QuickSaveSelectedForPublication(NewMacro18)
  .Run(varList1)
End With
Similar to setting the image quality in the 'Save As' operation,
how can I adjust the image quality in a publication macro?



Waiting for a response, thank you, everyone.

Tagged:

Best Answer

  • Answer ✓
    The function SaveBitmap existed in the DLL since 2019, but if you don't have it, then it can be implemented in the macro with some other helper functions. So, the new version of the macro will look like this:

        Public Function ConvertTo24Bpp(ByVal b1 As System.Drawing.Image) As System.Drawing.Image
            If b1.PixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb Then Return b1
            Dim b2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(b1.Size.Width, b1.Size.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
            Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(b2)
            g.DrawImage(b1, New System.Drawing.Point(0, 0))
            g.Dispose()
            Return b2
        End Function
    
    
        Private Function GetEncoder(ByVal format As System.Drawing.Imaging.ImageFormat) As System.Drawing.Imaging.ImageCodecInfo
            Dim codecs As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()
            Dim codec As System.Drawing.Imaging.ImageCodecInfo
            For Each codec In codecs
                If codec.FormatID = format.Guid Then
                    Return codec
                End If
            Next codec
            Return Nothing
        End Function
    
        ''' <summary>
        ''' Save bitmap to a file. The codec is selected automatically from the extension.
        ''' </summary>
        ''' <param name="img"></param>
        ''' <param name="sFileName"></param>
        ''' <param name="iCompression">JPEG compression</param>
        ''' <returns></returns>
        Private Function SaveBitmap(img As System.Drawing.Bitmap, sFileName As String, Optional iCompression As Long = 75) As Boolean
            If img.PixelFormat <> System.Drawing.Imaging.PixelFormat.Format24bppRgb Then img = ConvertTo24Bpp(img) 'ensure that we have 24bpp image
            'set correct codec by extension
            Dim format As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
            Select Case System.IO.Path.GetExtension(sFileName).ToLower
                Case ".bmp"
                    format = System.Drawing.Imaging.ImageFormat.Bmp
                Case ".jpg", ".jp2"
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
                Case ".tif", ".tiff"
                    format = System.Drawing.Imaging.ImageFormat.Tiff
                Case Else
                    format = System.Drawing.Imaging.ImageFormat.Png
            End Select
            Try
                If format.Guid = System.Drawing.Imaging.ImageFormat.Jpeg.Guid Then
                    'define compression for JPEG
                    Dim imgEncoder As System.Drawing.Imaging.ImageCodecInfo = GetEncoder(format)
                    ' Create an Encoder object based on the GUID
                    ' for the Quality parameter category.
                    Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
                    ' Create an EncoderParameters object.
                    ' An EncoderParameters object has an array of EncoderParameter
                    ' objects. In this case, there is only one
                    ' EncoderParameter object in the array.
                    Dim myEncoderParameters As New System.Drawing.Imaging.EncoderParameters(1)
                    'set Quality Factor of JPEG compression
                    Dim myEncoderParameter As New System.Drawing.Imaging.EncoderParameter(myEncoder, iCompression)
                    myEncoderParameters.Param.SetValue(myEncoderParameter,0)
                    img.Save(sFileName, imgEncoder, myEncoderParameters)
                Else
                    'default save
                    img.Save(sFileName, format)
                End If
                Return True
            Catch ex As System.Exception
                Throw ex 'pass up
            End Try
            Return False
        End Function
    
        Public Sub CustomQuickSaveForPublication()
            'get screenshot of the active window
            Dim window As Object = ThisApplication.ActiveWindow
            If window Is Nothing OrElse window.Document Is Nothing Then Exit Sub
            Dim im As McImage = window.ImageView.ImageToDisplay
            Dim mcDisplay As MediaCy.IQL.Display.Viewer.McDisplay = ThisApplication.Engine.CreateOperator("McDisplay", im)
            'use native image size
            mcDisplay.DisplayedWidth = im.Width
            mcDisplay.DisplayedHeight = im.Height
            mcDisplay.AutoZoomMode = MediaCy.IQL.Display.Viewer.mcAutoZoomMode.mazmBestFit
            Dim iHandle As Integer = mcDisplay.CreatePicture(True).Handle
            Dim img As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(iHandle))
    
            Dim quality As Integer = 99 'define quality in range 1-100
            Dim filename As String = "D:\MyPublication.jpg"'file name
            SaveBitmap(img,filename,quality)
        End Sub
    

    Yuri

Answers

  • Hi Kyle1996,

    QuickSave for publication uses quality 75 and this parameter cannot be adjusted. 
    If you want to adjust the quality, you can do it using a custom macro, like this:

        Public Sub CustomQuickSaveForPublication()
            'get screenshot of the active window
            Dim window As Object = ThisApplication.ActiveWindow
            If window Is Nothing OrElse window.Document Is Nothing Then Exit Sub
            Dim im As McImage = window.ImageView.ImageToDisplay
            Dim mcDisplay As MediaCy.IQL.Display.Viewer.McDisplay = ThisApplication.Engine.CreateOperator("McDisplay", im)
            'use native image size
            mcDisplay.DisplayedWidth = im.Width
            mcDisplay.DisplayedHeight = im.Height
            mcDisplay.AutoZoomMode = MediaCy.IQL.Display.Viewer.mcAutoZoomMode.mazmBestFit
            Dim iHandle As Integer = mcDisplay.CreatePicture(True).Handle
            Dim img As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(iHandle))
    
            Dim quality As Integer = 99 'define quality in range 1-100
            Dim filename As String = "D:\MyPublication.jpg"'file name
            Mediacy.Controls.Common.Vb.McImageUtils.SaveBitmap(img,filename,quality)
        End Sub
    

    Note, that you must add references to Mediacy.Controls.Common.Vb.dll and MediaCy.IQL.Display.Viewer.dll to your macro project!

    Yuri
  • Hi Yuri
    Thank you for your response.

    I tried it, and encountered this issue.

    I have added references to Mediacy.Controls.Common.Vb.dll, but the [.SaveBitmap] action is not available.
    My version of Mediacy.Controls.Common.Vb.dll is 3.1.0.0.


    Is there something wrong with my operation?

    Waiting for a response, thank you.
  • Hi Yuri
    Thank you for your response. It can run!
Sign In or Register to comment.