|
-
Jan 17th, 2004, 02:40 PM
#1
Thread Starter
Lively Member
Saving multiple pages? [ *Resolved*]
how can i add a page onto a tiff and save the bitmap?
VB Code:
Dim img As System.Drawing.Image
img = Image.FromFile(imgPath)
Dim fd As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList(0))
txtPages.Text = (img.GetFrameCount(fd).ToString)
img.SelectActiveFrame(fd, 0)
PB.Image = img
i know the amount of pages, so how would i add a frame ?
then just
Last edited by NeonBurner; Jan 18th, 2004 at 12:38 AM.
-
Jan 17th, 2004, 06:32 PM
#2
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!!
-
Jan 17th, 2004, 08:08 PM
#3
Thread Starter
Lively Member
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
-
Jan 17th, 2004, 08:48 PM
#4
Thread Starter
Lively Member
this code crashes on adding the second page can anyone figure it out?
VB Code:
Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders As ImageCodecInfo()
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length - 2
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If
Next j
Return Nothing
End Function
Private Sub AddPage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim multi As Bitmap
Dim page2 As Bitmap
Dim page3 As Bitmap
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
' Create three Bitmap objects.
multi = New Bitmap("C:\multi.tiff")
page2 = New Bitmap("C:\two.bmp")
page3 = New Bitmap("C:\three.bmp")
' Get an ImageCodecInfo object that represents the TIFF codec.
myImageCodecInfo = GetEncoderInfo("C:\multi.tiff")
' Create an Encoder object based on the GUID
' for the SaveFlag parameter category.
myEncoder = Encoder.SaveFlag
' 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.
myEncoderParameters = New EncoderParameters(1)
' Save the first page (frame).
myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.MultiFrame)))
myEncoderParameters.Param(0) = myEncoderParameter
multi.Save("C:\Multiframe.tiff", Imaging.ImageFormat.Tiff)
' Save the second page (frame).
myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.FrameDimensionPage)))
myEncoderParameters.Param(0) = myEncoderParameter
multi.SaveAdd(page2, myEncoderParameters)
' Save the third page (frame).
myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.FrameDimensionPage)))
myEncoderParameters.Param(1) = myEncoderParameter
multi.SaveAdd(page3, myEncoderParameters)
' Close the multiple-frame file.
myEncoderParameter = New EncoderParameter(myEncoder, (CLng(EncoderValue.Flush)))
myEncoderParameters.Param(1) = myEncoderParameter
multi.SaveAdd(myEncoderParameters)
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+.
-
Jan 17th, 2004, 11:14 PM
#5
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim img1, img2, tiffImg As Bitmap
img1 = Image.FromFile("C:\colormatrix.jpg")
img2 = Image.FromFile("C:\txtr.bmp")
tiffImg = New Bitmap(img1)
Dim encParams As New EncoderParameters(1)
' 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
Dim codecInfo As ImageCodecInfo = GetEncoderInfo("image/tiff")
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))
tiffImg.Save("C:\tiffFile.tiff", codecInfo, encParams)
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
tiffImg.SaveAdd(img2, encParams)
img1.Dispose()
img2.Dispose()
End Sub
Private Function GetEncoderInfo(ByVal mimeType As [String]) As ImageCodecInfo
Dim i As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For i = 0 To (encoders.Length - 1)
If (encoders(i).MimeType = mimeType) Then
Return encoders(i)
End If
Next i
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!!
-
Jan 18th, 2004, 12:34 AM
#6
Thread Starter
Lively Member
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:
Private Function GetEncoderInfo(ByVal mimeType As [String]) As ImageCodecInfo
Dim i As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For i = 0 To (encoders.Length - 1)
If (encoders(i).MimeType = mimeType) Then
Return encoders(i)
End If
Next i
End Function
Private Sub AddPage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim img As System.Drawing.Image
Dim img1, img2, imgBoth As Bitmap
Dim imgPath As String = "C:\one.tiff"
Dim img2Path As String = "C:\two.tiff"
Dim imgBothPath As String = "C:\Both.tiff"
Dim imgPages As Int16 ' page count for image1
img = Image.FromFile(imgPath) ' grab first tiff
Dim fd As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList(0))
imgPages = (img.GetFrameCount(fd)) ' set page count
img2 = Image.FromFile(img2Path) 'grab second image, assumming it has only 1 page
imgBoth = New Bitmap(img)
' Create an EncoderParameters object.
Dim encParams As New EncoderParameters(1)
' Get an ImageCodecInfo object that represents the TIFF codec.
Dim codecInfo As ImageCodecInfo = GetEncoderInfo("image/tiff")
'set the type of tiff
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))
imgBoth.Save(imgBothPath, codecInfo, encParams) ' save first page which is 0
'set tiff frame
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
Dim x As Int16 = 1
Do While x < imgPages ' save all previous pages in image 1
img.SelectActiveFrame(fd, x)
img1 = img
imgBoth.SaveAdd(img1, encParams)
x += 1
Loop
' append new page to tiff
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
imgBoth.SaveAdd(img2, encParams)
' Close the multiple-frame file.
encParams.Param(0) = New EncoderParameter(Encoder.SaveFlag, (CLng(EncoderValue.Flush)))
imgBoth.SaveAdd(encParams)
img.Dispose()
img1.Dispose()
img2.Dispose()
End Sub
Last edited by NeonBurner; Jan 18th, 2004 at 12:37 AM.
-
Mar 24th, 2010, 05:36 PM
#7
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|