Results 1 to 36 of 36

Thread: save JPG into file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    save JPG into file

    I would like to be able to save JPGs and/or PNGs or some other lossless image into my own files. I would like to manually store the picture size so that I can store multiple images into one file along with other data. What is a good way to do this? Is the 'Image' the only object I need? Thanks.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: save JPG into file

    I dont know if this is considered a bad way of doing it but, if I where to do something like this with the knowledge that I have, I would do the following:
    1. Create a class containing a private declared Bitmap array and add some properties to add/remove images.
    2. Add the serializable attribute to the class.
    3. Add a public function to binary serialize the class.
    Last edited by Atheist; Mar 8th, 2007 at 06:07 PM. Reason: fixed a minor miss-typing
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Not a bad idea, however I wanted to avoid binary output, I guess it could work. Basically, I want to create a XML file that also contains images. That way the users text data and the pictures can be saved in one file. I think I would prefer JPGs and PNGs because those are the top 2 formats. JPGs for great size, and PNGs perfect quality and good size.

    Would this function help? And I assume it has a ToStream, but I can not find it.
    FromStream Overloaded. Creates an Image object from the specified data stream.

    Quote Originally Posted by Atheist
    I dont know if this is considered a bad way of doing it but, if I where to do something like this with the knowledge that I have, I would do the following:
    1. Create a class containing a private declared Bitmap array and add some properties to add/remove images.
    2. Add the serializable attribute to the class.
    3. Add a public function to binary serialize the file.

    Also, I tried to load an image and display it, and it did not work. Do I need to do something else?
    Code:
    PictureBox1.Image.FromFile("c:\resume.jpg")
    PictureBox1.Update()
    PictureBox1.Refresh()
    Last edited by rex64; Mar 8th, 2007 at 06:13 PM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I am having some trouble because my IO stream is not in the right format, and I am not sure what I should do. I am a little new to IO still.

    C:\Reports\Jeff\VB\imageSaveTest\imageSaveTest\Form1.vb(111): Overload resolution failed because no accessible 'Save' can be called with these arguments:
    'Public Sub Save(stream As System.IO.Stream, format As System.Drawing.Imaging.ImageFormat)': Value of type 'System.IO.StreamWriter' cannot be converted to 'System.IO.Stream'.
    'Public Sub Save(filename As String, format As System.Drawing.Imaging.ImageFormat)': Value of type 'System.IO.StreamWriter' cannot be converted to 'String'.

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Dim testImage As Image
            'Image.FromHbitmap()
    
            ' Sets up an image object to be displayed.
            If Not (myImage Is Nothing) Then
                myImage.Dispose()
            End If
    
            ' Stretches the image to fit the pictureBox. 
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            myImage = New Bitmap("c:\resume.jpg")
            PictureBox1.ClientSize = New Size(100, 100)
            PictureBox1.Image = CType(myImage, Image)
    
    
            Dim myStream As StreamWriter
            myStream = New StreamWriter("c:\theImageTest.jpg") 'SaveFileDialog1.FileName)
            If Not (myStream Is Nothing) Then
                PictureBox1.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                'myStream.WriteLine(123)
            End If
    
            'PictureBox1.Image.FromFile("c:\StudyX.bmp")
            'PictureBox1.Update()
            'PictureBox1.Refresh()
    
            'Dim bmp As Bitmap = CType(Me.PictureBox1.Image, Bitmap)
            'MessageBox.Show("Before setting the pixel {5,5} to white color " & bmp.GetPixel(5, 5).ToString)
            'bmp.SetPixel(5, 5, Color.White)
            'Me.PictureBox1.Image = bmp
            'MessageBox.Show("After setting the pixel {5,5} to white color " & bmp.GetPixel(5, 5).ToString)
        End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: save JPG into file

    The Image property of the Picturebox must be assigned a value, but you where aaalmost doing it correctly. Heres how it should look:

    VB Code:
    1. PictureBox1.Image = Image.FromFile("c:\resume.jpg")
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Thanks for the info, that does clear things up quite a bit. Can you also check out post 4 and give a little guidance on my saving? Thanks.

    Quote Originally Posted by Atheist
    The Image property of the Picturebox must be assigned a value, but you where aaalmost doing it correctly. Heres how it should look:

    VB Code:
    1. PictureBox1.Image = Image.FromFile("c:\resume.jpg")
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: save JPG into file

    I think you should pass a FileStream to the .Save method.

    Here you go:
    VB Code:
    1. 'Create a new instance of the FileStream class
    2. Dim fs As New IO.FileStream("c:\NewImage.jpg", IO.FileMode.CreateNew, IO.FileAccess.Write, IO.FileShare.None)
    3. 'Pass the FileStream instance to the picturebox' Save method
    4. PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
    5. 'Close and dispose the FileStream object once we're done with it.
    6. fs.Close
    7. fs.Dispose()
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I already am using StreamWriter, because I think it is easier to work with? Is there any way I can convert between the two? Or how should I be doing this? Do I have to pick one or the other? I want to write text and images to one file.

    Quote Originally Posted by Atheist
    I think you should pass a FileStream to the .Save method.

    Here you go:
    VB Code:
    1. 'Create a new instance of the FileStream class
    2. Dim fs As New IO.FileStream("c:\NewImage.jpg", IO.FileMode.CreateNew, IO.FileAccess.Write, IO.FileShare.None)
    3. 'Pass the FileStream instance to the picturebox' Save method
    4. PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
    5. 'Close and dispose the FileStream object once we're done with it.
    6. fs.Close
    7. fs.Dispose()
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: save JPG into file

    Quote Originally Posted by rex64
    I already am using StreamWriter, because I think it is easier to work with? Is there any way I can convert between the two? Or how should I be doing this? Do I have to pick one or the other? I want to write text and images to one file.
    Well you cant compare the StreamWriter object and the FileStream object to see which one is easiest to work with, because they are two totally different things.
    The Save method saves the image to a file, and it needs to be passed a valid stream. A StreamWriter isnt a stream, it just writes to streams.
    Im kind of lost with what you want to achieve...I think I have to go back and read your original post again
    ´
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Well, maybe I can make it more clear. Here is something like what I would like the file to look like. Here is how the file will probably look:
    Code:
    <questionAnswerList>
    	<item>
                 <question>Who was the first President of the US?</question>
                 <answer>George Washington</answer>
                 <picture>PIC BINARY GOES HERE</picture>
                 <picture>PIC BINARY GOES HERE</picture>
    	</item>
    </questionAnswerList>
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Is there a better way I should be doing this?
    Quote Originally Posted by rex64
    Well, maybe I can make it more clear. Here is something like what I would like the file to look like. Here is how the file will probably look:
    Code:
    <questionAnswerList>
    	<item>
                 <question>Who was the first President of the US?</question>
                 <answer>George Washington</answer>
                 <picture>PIC BINARY GOES HERE</picture>
                 <picture>PIC BINARY GOES HERE</picture>
    	</item>
    </questionAnswerList>
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Is this a good XML file?

    EDIT:Can I use single quotes instead of double quotes?
    Code:
    <?xml version='1.0'?>
    <questionAnswerList>
    	<questionCount>4</questionCount>
    	<url>http://www.StudyX.com</url>
    	<item>
    		<id>1</id>
    		<question> Россия (Russia)</question>
    		<answer> Россия (Russia)A</answer>
    		<timesRight>0</timesRight>
    		<timesWrong>0</timesWrong>
    		<picture>PIC BINARY GOES HERE!!!!!!!!!!</picture>
    	</item>
    	<item>
    		<id>2</id>
    		<question>العالم العربي (Arabic)</question>
    		<answer>العالم العربي (Arabic)A</answer>
    		<timesRight>0</timesRight>
    		<timesWrong>0</timesWrong>
    	</item>
    	<item>
    		<id>3</id>
    		<question>ישראל (Israel)</question>
    		<answer>ישראל (Israel)A</answer>
    		<timesRight>0</timesRight>
    		<timesWrong>0</timesWrong>
    	</item>
    	<item>
    		<id>4</id>
    		<question>Ψ ψ Psi 	Ω ω Omega</question>
    		<answer>Ψ ψ Psi 	Ω ω Omega (Greek Answer)</answer>
    		<timesRight>0</timesRight>
    		<timesWrong>0</timesWrong>
    	</item>
    </questionAnswerList>
    Last edited by rex64; Mar 20th, 2007 at 12:30 PM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    What do you think of the XML in the last post?
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  14. #14
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save JPG into file

    Quote Originally Posted by rex64
    I would like to be able to save JPGs and/or PNGs or some other lossless image into my own files. I would like to manually store the picture size so that I can store multiple images into one file along with other data. What is a good way to do this? Is the 'Image' the only object I need? Thanks.
    Hi,

    You can use the SaveFileDialog with a SaveFileDialog.filter for that.
    Here's a link how to use, with an example;

    http://msdn2.microsoft.com/en-us/library/sfezx97z.aspx

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  15. #15
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    Did you ever figure this out?

  16. #16
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: save JPG into file

    Ken,

    Why don't you post what you are trying to do? If you are looking to do what rex was trying to do, it is definately possible, but it might not be the ideal solution. Off the top of my head, you could probably base64 encode the image and then store the encoded data in the XML in a CDATA element.

  17. #17
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Smile Re: save JPG into file

    I just got it to work.

    I used a fileStream to write the file.

    Code:
    Private wfStream As FileStream
    Private mBytes() As Byte
    
    
    
    wfStream = New FileStream(ReportFileName, FileMode.Create, FileAccess.Write)
    I wrote the text portion using:

    Code:
    mBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(strPDF)
    wfStream.Write(mBytes, 0, mBytes.Length)
    The images were written to the file using:

    Code:
    Dim retByte() As Byte
    
    Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)
    ReDim retByte(fs.Length)
    Dim binReader As BinaryReader = New BinaryReader(fs)
    retByte = binReader.ReadBytes(fs.Length)
    binReader.Close()
    fs.Close()
    
    'write the raw image data.
    wfStream.Write(retByte, 0, retByte.Length)

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    That is really cool, and helps a lot. Now I am trying to figure out how I should be writing the file and reading the file. I was thinking about using new line (vbCrlf?) characters to separate the image and text, however I was thinking about it some more, and realized that the image could have any characters in it.

    So, I think I will need to use only the XML codes <picture> to find the sections. This is easy when you are writing the image, however when you are reading the file, I am not sure. Should I read the file as one giant string? I am worried if I have 100 images in the file that things will get complicated. What do you recommend on reading in the file? Should I read it in as binary, and do I have to specify a maximum size? Thanks.

    Quote Originally Posted by Ken B
    I just got it to work. I used a fileStream to write the file...
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  19. #19
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    I'm creating pdf files, not xml. So this may not work.

    How about adding the image size before writing the image to the file. This way, you would know how many bytes to read to get the image.

    Code:
    <item>
        <question>Who was the first President of the US?</question>
        <answer>George Washington</answer>
        <size>74589</size>
        <picture>PIC BINARY GOES HERE</picture>
    </item>

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    That could work, but I would prefer to have a more robust file reading engine that would handle corrupted files. This system would get messed up if one of the pictures or anything in the file gets corrupt. What is the best way to make a reliable file reading algorithm?

    Quote Originally Posted by Ken B
    I'm creating pdf files, not xml. So this may not work.

    How about adding the image size before writing the image to the file. This way, you would know how many bytes to read to get the image.

    Code:
    <item>
        <question>Who was the first President of the US?</question>
        <answer>George Washington</answer>
        <size>74589</size>
        <picture>PIC BINARY GOES HERE</picture>
    </item>
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I still need some help with post 20. Also, I am attempting to read in the data, however I am getting the same image loading in each time. I guess I have to manually adjust the stream read point some how, but I am not sure the best way to do that. When I write out the image data I am just using the .Save method, so I am not sure exactly the size.

    Code:
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Dim wfStream As FileStream
            Dim reportfileName As String = "c:\test.txt"
    
            wfStream = New FileStream(reportfileName, FileMode.Open, FileAccess.Read)
    
            PictureBox1.Image = PictureBox1.Image.FromStream(wfStream)    ', System.Drawing).Imaging.ImageFormat.Jpeg)
            PictureBox2.Image = PictureBox2.Image.FromStream(wfStream)    'PictureBox2.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    
            wfStream.Close()
    
            'System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
    
        End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  22. #22
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: save JPG into file

    All i can say is:
    Place the location or name of the image in your .xml. This way when you load data you dont load the full image. When you wanna show that image. You can just load it like: picturebox1.image = image.fromfile(DATAFROMXML)
    And you are done. If you dont want your people seeing those images when loading them use the filter in the openfiledialog or save your images as hidden files or in a special map.

    Hope it helps

    Greets

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I am creating a skin file and also a image database file, both of witch really need to be in one file in order to be slick. So, I do not believe your method would help me?
    Quote Originally Posted by ovanwijk
    All i can say is:
    Place the location or name of the image in your .xml. This way when you load data you dont load the full image. When you wanna show that image. You can just load it like: picturebox1.image = image.fromfile(DATAFROMXML)
    And you are done. If you dont want your people seeing those images when loading them use the filter in the openfiledialog or save your images as hidden files or in a special map.

    Hope it helps

    Greets
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  24. #24
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: save JPG into file

    Well i dont know how big the database is gonna be but i personal dont like it to place the image itself in a database. I am currently bussy with an acces system with pictures (card + picture display on a monitor). And i place images in a map sorted on year of birth from the members and put the filename in the database.

    But i think this is not what you want, anyway i dont think its possible to put it inside a .xml file.

    Greets

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    If not an XML File I will just put it in my own custom file format. But I would really like the images to be built into the file. I have thousands of people who use this software, and I want it to be easy for them, if they have to have hundreds of pictures laying around their hard drive, that will complicate things.
    Quote Originally Posted by ovanwijk
    ...But i think this is not what you want, anyway i dont think its possible to put it inside a .xml file...
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I am trying to save the text with the images. I can not get the bytes. I tried convert, but that did not work either. Anyways, if you could help me write the text before and after the image, and also if you could help me read it in nicely, I would really appreciate it. I know this should not be that hard, but I am having trouble figuring out a good way to do this.


    Error 18 Value of type 'String' cannot be converted to '1-dimensional array of Byte'. C:\Reports\Jeff\VB\study\StudyX.NET\frmPictureViewer.vb 117 18 studyX

    Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            'Dim testImage As Image
            'Image.FromHbitmap()
    
            ' Sets up an image object to be displayed.
            If Not (myImage Is Nothing) Then
                myImage.Dispose()
            End If
    
            ' Stretches the image to fit the pictureBox. 
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            myImage = New Bitmap("c:\resume.jpg")
            PictureBox1.ClientSize = New Size(100, 100)
            PictureBox1.Image = CType(myImage, Image)
    
    
            'Dim myStream As StreamWriter
            'myStream = New StreamWriter("c:\theImageTest.jpg") 'SaveFileDialog1.FileName)
            'If Not (myStream Is Nothing) Then
            '    PictureBox1.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            '    'myStream.WriteLine(123)
            'End If
    
    
            'Create a new instance of the FileStream class
            Dim fs As New IO.FileStream("c:\NewImage.jpg", IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.None)
            'Pass the FileStream instance to the picturebox' Save method
            fs.Write("<Image1start>", 0, 1000)
            PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
            fs.Write("<Image1end>", 0, 1000)
            fs.Write("<Image2start>", 0, 1000)
            PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
            fs.Write("<Image2end>", 0, 1000)
            'Close and dispose the FileStream object once we're done with it.
            fs.Close()
            'fs.Dispose()
    
            'PictureBox1.Image.FromFile("c:\StudyX.bmp")
            'PictureBox1.Update()
            'PictureBox1.Refresh()
    
            'Dim bmp As Bitmap = CType(Me.PictureBox1.Image, Bitmap)
            'MessageBox.Show("Before setting the pixel {5,5} to white color " & bmp.GetPixel(5, 5).ToString)
            'bmp.SetPixel(5, 5, Color.White)
            'Me.PictureBox1.Image = bmp
            'MessageBox.Show("After setting the pixel {5,5} to white color " & bmp.GetPixel(5, 5).ToString)
        End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  27. #27
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    This code should insert an image into a file.

    Code:
        Dim retByte() As Byte
        If Not File.Exists(ImageFileName) Then Return False
    
        Try
          Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)
          ReDim retByte(fs.Length)
          Dim binReader As BinaryReader = New BinaryReader(fs)
          retByte = binReader.ReadBytes(fs.Length)
          binReader.Close()
          fs.Close()
    
        Catch exIo As IOException
          MessageBox.Show("IOException in cPdf.loadImageFile." & Chr(10) & exIo.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
    
        Catch ex As Exception
          MessageBox.Show("Exception in cPdf.loadImageFile." & Chr(10) & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
        End Try

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    That looks great for reading, but I do not think it writes? Do you do the same thing basically on your write also? Let me know. I think you may have posted the wrong code
    Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)

    Quote Originally Posted by Ken B
    This code should insert an image into a file.

    Code:
        Dim retByte() As Byte
        If Not File.Exists(ImageFileName) Then Return False
    
        Try
          Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)
          ReDim retByte(fs.Length)
          Dim binReader As BinaryReader = New BinaryReader(fs)
          retByte = binReader.ReadBytes(fs.Length)
          binReader.Close()
          fs.Close()
    
        Catch exIo As IOException
          MessageBox.Show("IOException in cPdf.loadImageFile." & Chr(10) & exIo.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
    
        Catch ex As Exception
          MessageBox.Show("Exception in cPdf.loadImageFile." & Chr(10) & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
        End Try
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  29. #29
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    Hey, it was 6:55am, I was still working on my first cup of Coffee.

    You will of course need to write it.

    This should work. I took this code from different parts of a pdf creator I wrote last year, but I think all the code you need is here.

    Code:
      Private wfStream As FileStream
    
          wfStream = New FileStream(ReportFileName, FileMode.Create, FileAccess.Write)
    
    
    WriteToFile("Some text before the image")
    
        Dim retByte() As Byte
        Try
          Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)
          ReDim retByte(fs.Length)
          Dim binReader As BinaryReader = New BinaryReader(fs)
          retByte = binReader.ReadBytes(fs.Length)
          binReader.Close()
          fs.Close()
    
        Catch exIo As IOException
          MessageBox.Show("IOException in cPdf.loadImageFile." & Chr(10) & exIo.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
    
        Catch ex As Exception
          MessageBox.Show("Exception in cPdf.loadImageFile." & Chr(10) & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
        End Try
    
        'write the raw image data.
        wfStream.Write(retByte, 0, retByte.Length)
    
    WriteToFile("Some text after the image")
    
        wfStream.Dispose()
    
    
    
    
      Private Sub WriteToFile(ByVal Text As String)
    
        For i As Integer = 0 To Text.Length - 1
          wfStream.WriteByte(Asc(Text.Substring(i, 1)))
        Next i
    
      End Sub

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    Excellent example. I am getting much closer now. What is the best way to write the length to the file? Also, how would I read it in.

    Code:
    wfStream = New IO.FileStream("c:\skinOutput.jpg", FileMode.Create)
            WriteToFile("Skin File Type: 1")
            WriteToFile("Background Color: 0")
            WriteToFile("Font Color: 0")
            WriteToFile("Font Color: 0")
    
            WriteToFile("<MainInterface>")
            PictureBox1.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</MainInterface>")
    
            WriteToFile("<ViewAll>")
            PictureBox2.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</ViewAll>")
    
            WriteToFile("<Dialog>")
            PictureBox3.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</Dialog>")
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  31. #31
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    To you need to write the image size? You should be able to determine it by subtracting the starting position of </MainInterface> from the ending position of <MainInterface>

    Reading it in, I'm not so sure. This code is from a pdf creator, so I never had to really read it. I would test it using very small images.

    Do you have vb6? I wrote the following vb6 code to read a pdf file one character at a time and write each character to a file with the character position, a couple of spaces, the character and a carriage return. It help with testing.

    Code:
    Private Sub cmdCreateLog_Click()
    
      Dim lngLocation As Long
      Dim strLine     As String
      
      Open "c:\Test.pdf" For Binary As #1   ' Open file just created.
      Open "c:\pdfTest.txt" For Output As #2
      
    '  Open "c:\testNew.pdf" For Binary As #1   ' Open file just created.
    '  Open "c:\pdfNewLog.txt" For Output As #2
    
    '  Open "c:\testOld.pdf" For Binary As #1   ' Open file just created.
    '  Open "c:\pdfOldLog.txt" For Output As #2
    
      Do While lngLocation < LOF(1)   ' Loop until end of file.
         strLine = Input(1, #1)    ' Read character into variable.
         lngLocation = Loc(1)   ' Get current position within file.
         If strLine = vbCr Then
           Beep
         End If
         Print #2, lngLocation & "     " & Replace(Replace(strLine, vbLf, "line feed"), vbCr, "carriage return") & vbCrLf;
    
      Loop
      Close #1   ' Close file.
      Close #2
    
      End
    If you convert the above code, please send me a copy of it. I never got around to it.

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I did some research, is there no way to find out the size of the image if the length function is not specified? It does not look good from what I saw.
    http://bytes.com/forum/thread348479.html
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  33. #33
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    ???

    What size are you talking about? File size or image width and Height?

    File size of an image:
    Code:
    Dim fs As FileStream = New FileStream(ImageFileName, FileMode.Open)
    ReDim retByte(fs.Length)
    Image width and height as a size:
    This is c# code, but you should be able to convert it.
    Code:
    Bitmap bit = new Bitmap(imageFileName);
    imageSize = new Size(bit.Width, bit.Height);
    bit.Dispose();

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    What is the best way to check for EOF? Peek does not appear to be available. Also, is this a good way for me to be reading the file?

    Code:
        Private Function ReadToString(ByRef stream As IO.FileStream)
            Dim theByte As Byte
            Dim theString As String
            While (theByte <> 10 And stream.peek <> -1)
                stream.ReadByte()
                theString = theString & theByte
            End While
            Return theString
        End Function
    
        Private Sub btn_LoadSkin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadSkin.Click
            inStream = New IO.FileStream("c:\skinOutput.sxs", FileMode.Open)
            Dim skinType As Integer = ReadToString(inStream)
            Dim backgroundColor As Integer = ReadToString(inStream)
            Dim fontColor1 As Integer = ReadToString(inStream)
            Dim fontColor2 As Integer = ReadToString(inStream)
    
            Dim dummy As Integer = ReadToString(inStream) 'main start   
            PictureBox1.Image.FromStream(inStream)
            dummy = ReadToString(inStream) 'main stop
    
            dummy = ReadToString(inStream) 'ViewAll Start
            PictureBox2.Image.FromStream(inStream)
            dummy = ReadToString(inStream) 'ViewAll stop
    
            dummy = ReadToString(inStream) 'Dialog Start
            PictureBox3.Image.FromStream(inStream)
            dummy = ReadToString(inStream) 'Dialog stop
        End Sub
    
    'writing file (seems to be working)............
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            wfStream = New IO.FileStream("c:\skinOutput.sxs", FileMode.Create)
            WriteToFile("Skin File Type: 1")
            WriteToFile("Background Color: 0")
            WriteToFile("Font Color: 0")
            WriteToFile("Font Color: 0")
    
    
            WriteToFile("<MainInterface>")
            PictureBox1.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</MainInterface>")
    
            WriteToFile("<ViewAll>")
            PictureBox2.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</ViewAll>")
    
            WriteToFile("<Dialog>")
            PictureBox3.Image.Save(wfStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            WriteToFile("</Dialog>")
    
    
    
            wfStream.Close()
    
        End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  35. #35
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: save JPG into file

    This will read a image into a file, with some format information before it. The image itself will be inside of the <image></image> tags. I have no idea if any of these tags are valid xml tags.


    Create a new project, add one button called cmdImage.

    Code:
    Public Class Form1
    
      Private wfStream As IO.FileStream 'File stream used to write file.
    
      Private Sub cmdImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateFile.Click
    
        'Filetype mff I just made up. You can use what ever you want.
        wfStream = New IO.FileStream("c:\temp\test.mff", IO.FileMode.Create, IO.FileAccess.Write)
    
        Dim imageSize = 0
    
        Dim retByte() As Byte
    
        Dim strImageFileName = "c:\temp\test.jpg"
        If Not System.IO.File.Exists(strImageFileName) Then
          MessageBox.Show("The file '" & strImageFileName & "' is missing")
          Exit Sub
        End If
    
        'Read the image file.
        Try
          Dim fs As IO.FileStream = New IO.FileStream(strImageFileName, IO.FileMode.Open)
          ReDim retByte(fs.Length)
          imageSize = fs.Length
          Dim binReader As IO.BinaryReader = New IO.BinaryReader(fs)
          retByte = binReader.ReadBytes(fs.Length)
          binReader.Close()
          fs.Close()
    
        Catch exIo As IO.IOException
          MessageBox.Show("IOException in cPdf.loadImageFile." & Chr(10) & exIo.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Exit Sub
    
        Catch ex As Exception
          MessageBox.Show("Exception in cPdf.loadImageFile." & Chr(10) & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          Exit Sub
        End Try
    
        WriteToFile("<imagename>1</imagename>")
        WriteToFile("<imagesize>" & imageSize & "</imagesize>")
        WriteToFile("<image>")
        'write the raw image data.
        wfStream.Write(retByte, 0, retByte.Length)
        WriteToFile("</image>")
    
        wfStream.Close()
        wfStream.Dispose()
    
        MessageBox.Show("Done")
    
      End Sub
    
      Private Sub WriteToFile(ByVal Text As String)
    
        For i As Integer = 0 To Text.Length - 1
          wfStream.WriteByte(Asc(Text.Substring(i, 1)))
        Next i
    
      End Sub
    
    End Class

  36. #36

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: save JPG into file

    I think you may have misunderstood me. I can write a skin file that includes text and then multiple images, but I can not read it. In your example, you read an image file and then wrote a combo file, but you did not read a combo file (I don't think?). Let me know. Also, do you know of good places to get examples of this kind of stuff?

    Quote Originally Posted by Ken B
    This will read a image into a file, with some format information before it..
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

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