Results 1 to 7 of 7

Thread: Saving multiple pages? [ *Resolved*]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68

    Saving multiple pages? [ *Resolved*]

    how can i add a page onto a tiff and save the bitmap?

    VB Code:
    1. Dim img As System.Drawing.Image
    2.         img = Image.FromFile(imgPath)
    3.         Dim fd As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList(0))
    4.  
    5.         txtPages.Text = (img.GetFrameCount(fd).ToString)
    6.         img.SelectActiveFrame(fd, 0)
    7.         PB.Image = img

    i know the amount of pages, so how would i add a frame ?
    then just

    VB Code:
    1. img.Save("C:\test.tif")
    Last edited by NeonBurner; Jan 18th, 2004 at 12:38 AM.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    sorry if I cant be of anyhelp, I'm just guessing:
    try Image.SaveAdd(). MSDN says : The SaveAdd method adds a frame to a file or stream specified in a previous call to the Save method. Use this method to save selected frames from a multiple-frame image to another multiple-frame image.

    also, I believe this would be the correct way of saving a tiff file. I THINK that if you dont specify the second parameter, VB will just save it as a JPEG file (even if it has a .tiff extension... double check on that though)

    img.Save(filename, imaging.ImageFormat.Tiff)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    yeah i was messing with that for a bit but SaveAdd expects a Imaging.EncoderParameters arguement, so when i research that it gives me these members

    http://msdn.microsoft.com/library/de...classtopic.asp

    so i been trying to figure out how to add Imaging.EncoderParameters

    i found some C# examples , that i'm working on converting, if i get it i'll post it

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    this code crashes on adding the second page can anyone figure it out?

    VB Code:
    1. Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
    2.         Dim j As Integer
    3.         Dim encoders As ImageCodecInfo()
    4.         encoders = ImageCodecInfo.GetImageEncoders()
    5.         For j = 0 To encoders.Length - 2
    6.             If encoders(j).MimeType = mimeType Then
    7.                 Return encoders(j)
    8.             End If
    9.         Next j
    10.         Return Nothing
    11.     End Function
    12.  
    13.     Private Sub AddPage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    14.  
    15.         Dim multi As Bitmap
    16.         Dim page2 As Bitmap
    17.         Dim page3 As Bitmap
    18.  
    19.         Dim myImageCodecInfo As ImageCodecInfo
    20.         Dim myEncoder As Encoder
    21.         Dim myEncoderParameter As EncoderParameter
    22.         Dim myEncoderParameters As EncoderParameters
    23.  
    24.         ' Create three Bitmap objects.
    25.         multi = New Bitmap("C:\multi.tiff")
    26.         page2 = New Bitmap("C:\two.bmp")
    27.         page3 = New Bitmap("C:\three.bmp")
    28.  
    29.         ' Get an ImageCodecInfo object that represents the TIFF codec.
    30.         myImageCodecInfo = GetEncoderInfo("C:\multi.tiff")
    31.  
    32.         ' Create an Encoder object based on the GUID
    33.         ' for the SaveFlag parameter category.
    34.         myEncoder = Encoder.SaveFlag
    35.  
    36.         ' Create an EncoderParameters object.
    37.         ' An EncoderParameters object has an array of EncoderParameter
    38.         ' objects. In this case, there is only one
    39.         ' EncoderParameter object in the array.
    40.         myEncoderParameters = New EncoderParameters(1)
    41.  
    42.         ' Save the first page (frame).
    43.         myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.MultiFrame)))
    44.         myEncoderParameters.Param(0) = myEncoderParameter
    45.  
    46.         multi.Save("C:\Multiframe.tiff", Imaging.ImageFormat.Tiff)
    47.  
    48.         ' Save the second page (frame).
    49.         myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.FrameDimensionPage)))
    50.         myEncoderParameters.Param(0) = myEncoderParameter
    51.         multi.SaveAdd(page2, myEncoderParameters)
    52.  
    53.         ' Save the third page (frame).
    54.         myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.FrameDimensionPage)))
    55.         myEncoderParameters.Param(1) = myEncoderParameter
    56.         multi.SaveAdd(page3, myEncoderParameters)
    57.  
    58.         ' Close the multiple-frame file.
    59.         myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.Flush)))
    60.         myEncoderParameters.Param(1) = myEncoderParameter
    61.         multi.SaveAdd(myEncoderParameters)
    62.  
    63.     End Sub

    with the following error

    A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll

    Additional information: A generic error occurred in GDI+.

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    I'm sorry I dont understand what I'm doing, that's why there are no comments in the code... but I managed to create a multi frame tiff file. I used some code from CodeProject.com

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim img1, img2, tiffImg As Bitmap
    3.  
    4.         img1 = Image.FromFile("C:\colormatrix.jpg")
    5.         img2 = Image.FromFile("C:\txtr.bmp")
    6.         tiffImg = New Bitmap(img1)
    7.  
    8.         Dim encParams As New EncoderParameters(1)
    9. ' You could use your method of getting the codec from file instead of using this.. but I guess this is better since you dont need to have a file to get the codec
    10.         Dim codecInfo As ImageCodecInfo = GetEncoderInfo("image/tiff")
    11.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))
    12.  
    13.  
    14.         tiffImg.Save("C:\tiffFile.tiff", codecInfo, encParams)
    15.  
    16.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
    17.         tiffImg.SaveAdd(img2, encParams)
    18.  
    19.  
    20.  
    21.  
    22.         img1.Dispose()
    23.         img2.Dispose()
    24.     End Sub
    25.  
    26.     Private Function GetEncoderInfo(ByVal mimeType As [String]) As ImageCodecInfo
    27.         Dim i As Integer
    28.         Dim encoders() As ImageCodecInfo
    29.         encoders = ImageCodecInfo.GetImageEncoders()
    30.  
    31.         For i = 0 To (encoders.Length - 1)
    32.             If (encoders(i).MimeType = mimeType) Then
    33.                 Return encoders(i)
    34.             End If
    35.         Next i
    36.     End Function

    change the filenames only HTH should be a good enough example


    this was the codeproject page btw: http://www.codeproject.com/vb/net/fa...asp?print=true
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    its the same code though, except how you originally save it, anyway i added some code to add all the frames of the first tiff and then close the file after everythings added, tested works great
    heres the code with some more comments

    VB Code:
    1. Private Function GetEncoderInfo(ByVal mimeType As [String]) As ImageCodecInfo
    2.         Dim i As Integer
    3.         Dim encoders() As ImageCodecInfo
    4.         encoders = ImageCodecInfo.GetImageEncoders()
    5.  
    6.         For i = 0 To (encoders.Length - 1)
    7.             If (encoders(i).MimeType = mimeType) Then
    8.                 Return encoders(i)
    9.             End If
    10.         Next i
    11.     End Function
    12.  
    13.     Private Sub AddPage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    14.  
    15.         Dim img As System.Drawing.Image
    16.         Dim img1, img2, imgBoth As Bitmap
    17.         Dim imgPath As String = "C:\one.tiff"
    18.         Dim img2Path As String = "C:\two.tiff"
    19.         Dim imgBothPath As String = "C:\Both.tiff"
    20.  
    21.         Dim imgPages As Int16 ' page count for image1
    22.         img = Image.FromFile(imgPath) ' grab first tiff
    23.  
    24.         Dim fd As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList(0))
    25.         imgPages = (img.GetFrameCount(fd)) ' set page count
    26.  
    27.         img2 = Image.FromFile(img2Path) 'grab second image, assumming it has only 1 page
    28.         imgBoth = New Bitmap(img)
    29.  
    30.         ' Create an EncoderParameters object.
    31.         Dim encParams As New EncoderParameters(1)
    32.  
    33.         ' Get an ImageCodecInfo object that represents the TIFF codec.
    34.         Dim codecInfo As ImageCodecInfo = GetEncoderInfo("image/tiff")
    35.         'set the type of tiff
    36.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))
    37.         imgBoth.Save(imgBothPath, codecInfo, encParams) ' save first page which is 0
    38.         'set tiff frame
    39.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
    40.  
    41.         Dim x As Int16 = 1
    42.         Do While x < imgPages ' save all previous pages in image 1
    43.             img.SelectActiveFrame(fd, x)
    44.             img1 = img
    45.             imgBoth.SaveAdd(img1, encParams)
    46.             x += 1
    47.         Loop
    48.  
    49.         ' append new page to tiff
    50.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
    51.         imgBoth.SaveAdd(img2, encParams)
    52.  
    53.         ' Close the multiple-frame file.
    54.         encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, (CLng(EncoderValue.Flush)))
    55.         imgBoth.SaveAdd(encParams)
    56.  
    57.         img.Dispose()
    58.         img1.Dispose()
    59.         img2.Dispose()
    60.  
    61.     End Sub
    Last edited by NeonBurner; Jan 18th, 2004 at 12:37 AM.

  7. #7
    New Member
    Join Date
    Mar 2010
    Posts
    1

    Re: Saving multiple pages? [ *Resolved*]

    This was very helpful; here is the code in C#:

    HTML Code:
                fileInfo = getTiffFileInfo();
                bitmap = new System.Drawing.Bitmap(fileInfo.FullName);
                size = bitmap.Size;
                frameDimension = new System.Drawing.Imaging.FrameDimension(bitmap.FrameDimensionsList[0]);
                
                imageCodecInfo = GetEncoderInfo(ImageFormat.Tiff);
                encoderParameters = new EncoderParameters();
                
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, 
                                (long)EncoderValue.MultiFrame);
    
                _frameCount = bitmap.GetFrameCount(frameDimension);
                for (int _i = 0; _i < _frameCount; _i++)
                {
                    bitmap.SelectActiveFrame(frameDimension, _i);
                    switch (_i)
                    {
                        case 0:
                            newBitmap = new Bitmap((Image)bitmap.Clone(), size.Width, size.Height);
                            newBitmap.Save(newFileInfo.FullName, imageCodecInfo, encoderParameters);
                            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
                                (long)EncoderValue.FrameDimensionPage); 
                            break;
    
                        default:
                            newBitmap.SaveAdd((Image)bitmap.Clone(),encoderParameters);
                            break;
                    }
    
                }
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
                    (long)EncoderValue.Flush);
                newBitmap.SaveAdd(encoderParameters);
                newBitmap.Dispose();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width