locked
add text to tiff file keeping initial picture settings (colors, size, etc) RRS feed

  • Question

  • i've created in VB script to add file name to a photo, and unfortunately enclosed version change colors map & file size,

    how to keep it without changes?

    Imports System
    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Imaging

    Public Class Form1
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If Me.TextBox1.Text <> "" Then
                Dim FolderBrowserDialogA As New FolderBrowserDialog()
                If FolderBrowserDialogA.ShowDialog = DialogResult.OK Then
                    Me.txtBackupFilePath.Text = FolderBrowserDialogA.SelectedPath
                    Dim di As New DirectoryInfo(FolderBrowserDialogA.SelectedPath)
                    Dim fiArr As FileInfo() = di.GetFiles()
                    Dim sfd As FileInfo, nF, mH, mW
                    nF = 0
                    For Each sfd In fiArr

                        If sfd.Name Like "*.tiff" And sfd.Name Like "*" & Me.TextBox1.Text & "*" Then
                            Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")

                            Dim original As New Bitmap(Image.FromFile(FolderBrowserDialogA.SelectedPath & "\" & sfd.Name, True))

                            Dim encList As EncoderParameters = original.GetEncoderParameterList(info.Clsid)
                            Dim encParams As EncoderParameter() = encList.Param
                            Dim ep As New EncoderParameters(3)


                            ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, CLng(System.Drawing.Imaging.EncoderValue.CompressionNone)) '

                            ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 32L) '
                            ep.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)

                            Dim g As Graphics = Graphics.FromImage(original)
                            Dim f As Double = 40 / 3662 * original.Height
                            Dim myFont As New Font("Arial", f)


                            Dim myBrush As New SolidBrush(Color.Black)
                            Dim stringFormat As New StringFormat()
                            If original.Width > original.Height Then
                                stringFormat.FormatFlags = StringFormatFlags.DirectionVertical
                                mH = 10 / 3662 * original.Height
                                mW = original.Height - Len(sfd.Name) * f
                            Else
                                stringFormat.FormatFlags = StringFormatFlags.NoClip
                                mH = original.Width - Len(sfd.Name) * f
                                mW = original.Height - f * 1.5
                            End If
                            Dim pointF As New PointF(mH, mW)

                            g.DrawString(sfd.Name, myFont, myBrush, pointF, stringFormat)

                            g.Equals(ColorMapType.Default)
                            g.Dispose()
                            Dim tP As String = FolderBrowserDialogA.SelectedPath & "\" & Mid(sfd.Name, 1, Len(sfd.Name) - 5) & "T.tiff"



                            original.Save(tP, info, ep)
                            original.Dispose()
                            nF = nF + 1
                        End If

                    Next sfd
                    MsgBox(nF & " files are updated")
                End If
            Else
                MsgBox("please enter file mask")
            End If
        End Sub
        Public Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
            Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
            For j As Integer = 0 To encoders.Length - 1
                If encoders(j).MimeType = mimeType Then
                    Return encoders(j)
                End If
            Next

            Throw New Exception(mimeType & " mime type not found in ImageCodecInfo")
        End Function

     
    Thursday, October 20, 2016 8:04 PM

Answers

  • Hi Alexander,

    >>i tested your script, and it changed the size...

    From my side, it doesn't change the size.

    >>how to add text to CMYK image & save it as CMYK?

    Here I found a similar thread, please have a look.

    How to add text to images with the CMYK ColorSpace in .NET 4

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AlexanderKal Monday, October 24, 2016 6:48 PM
    Monday, October 24, 2016 1:25 AM

All replies

  •  Hi AlexanderKal,

    >>I've created in VB script to add file name to a photo, and unfortunately enclosed version change colors map & file size, how to keep it without changes?

    I've tested with your code,  When you create the EncoderParameter  specify the Quality/Color encoder, and the desired compression level. So the picture will change the size and color of varying degrees.

    If you want to keep it without changes, Well in GDI+ you would read in the file using a Image class and then use the Graphics class to add text to it. Something like:

    Dim fs As New FileStream("C:\Users\XXX\Pictures\Saved Pictures\Capture.tiff", FileMode.Open, FileAccess.Read)
    Dim image__1 As Image = Image.FromStream(fs)
    fs.Close()
    
    Dim b As New Bitmap(image__1)
    Dim graphics__2 As Graphics = Graphics.FromImage(b)
    graphics__2.DrawString("Hello", Me.Font, Brushes.Red, 0, 0)
    
    b.Save("C:\Users\XXX\Pictures\Saved Pictures\somepic.tiff", image__1.RawFormat)
    
    image__1.Dispose()
    b.Dispose()

    Best regards.

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Kristin Xie Friday, October 21, 2016 6:00 AM
    Friday, October 21, 2016 5:55 AM
  • many thanks for your help,

    unfortunately for me it is changing the file: my file has size ~70 mb and the code decreases it to 17mb,

    could you help with the size?

    Friday, October 21, 2016 6:40 AM
  • @AlexanderKal

    Have your read my previous reply?

    Yes I also tested it, the result just as you said. But have you try to use the Graphics class to add text to tiff file?

    Have a nice day!

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 21, 2016 6:58 AM
  • Kristin,

    thanks for your kind help,

    i tested your script, and it changed the size...

    i can send the picture... i maybe  do some wrong action...

    best wishes,

    Alexander

    Friday, October 21, 2016 4:16 PM
  • Could be the color change/file resize be caused by CMYK change to RGB?
    Friday, October 21, 2016 8:58 PM
  • how to add text to CMYK image & save it as CMYK?
    Friday, October 21, 2016 9:01 PM
  • Hi Alexander,

    >>i tested your script, and it changed the size...

    From my side, it doesn't change the size.

    >>how to add text to CMYK image & save it as CMYK?

    Here I found a similar thread, please have a look.

    How to add text to images with the CMYK ColorSpace in .NET 4

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AlexanderKal Monday, October 24, 2016 6:48 PM
    Monday, October 24, 2016 1:25 AM