CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    Can save images to a structured text file C# ?

    Suppose structured

    public struct stStaff
    {
    public WhatType? Picture; // Pictures 3x4 (what kind for pictures ?)
    public string FullName; // FullName
    public DateTime BirthDay; // Birthday

    }

    Can save images to a structured text file (note: do not store the path of the image), to save the image of the structure of text files will have style? one example of this?
    Last edited by dongtrien; October 18th, 2012 at 06:35 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Can save images to a structured text file C# ?

    I'd consider using XML here, as it can provide much more flexibility with this type of info

  3. #3
    Join Date
    Jul 2012
    Posts
    90

    Re: Can save images to a structured text file C# ?

    Normally, pictures (jpg, png, tiff, etc.) are binary files. The problem comes when you try to represent binary as a string. if a 0 is encountered in the binary data (and it likely will be) it causes problems. This is due to the way strings are represented internally. a string is a series of character values (these are either bytes or longs depending on the encoding used to write the string) terminated by a 0 byte value. In order to represent binary values in text you will need to encode it using an encoding that will convert non-displayable characters and 0 byte values to a value allowed in a string (most use either Hex encoding or base64).

    Hex encoding doubles the number of bytes needed in the original binary value. base64 adds one third to the length of the original binary value.

    If you are mixing text and binary values in a file, it is much more efficient to use a binary file (you can write the byte values of the text and the actual binary value for the binary data. However, in most implementations, unlike a structured text file, your code will have to manage the content of the file (this is normally done by adding a header to the file that gives the offset addresses of the elements recorded in the file).

    .Net even has a binary serializer that you can use to do this for you.
    Last edited by CGKevin; October 21st, 2012 at 01:33 PM.

  4. #4
    Join Date
    Sep 2007
    Posts
    405

    Re: Can save images to a structured text file C# ?

    Suppose I have the structure
    Code:
    public struct st Staff 
            {
                public Bitmap Picture;    // Picturs 3x4
                public string FullName;   // Full name
                public DateTime BirthDay; // Bỉthday
    
            }
    in my save button code
    Code:
    Path = @"C:\data\database.dat";
    FileStream fs;
    try
    {
        fs = new FileStream(path, FileMode.Create);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
        return;
    }
     
        BinaryWriter bw = new BinaryWriter(fs);
     
    
    Staff = new Staff();
    Staff.Picture = (Bitmap)pictureBox1.Image;
    Staff.FullName = txtFullName.Text;
    Staff.BirthDay = DateTime.Parse(mskNgaysinh.Text);
     
    //ClsNhanvien Nhanvien;  // Save
    for (int i = 0; i < list.Count; i++)
    {
       Staff = (Staff)list[i];
       bw.Write(Staff.Picture);  // warning error: The best overloaded method match for 
                            //'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
       bw.Write(Staff.FullName);
       bw.Write(Staff.BirthDay.ToBinary());
    }
     
    bw.Flush();
    bw.Close();
    fs.Close();
    I get an error at this line
    bw.Write(Staff.Picture); // warning error: The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
    I wonder in the text file can save the Bitmap type? or to switch to the new chain store? if it is correct on how?
    Last edited by cjard; November 23rd, 2012 at 04:21 AM.

  5. #5
    Join Date
    Jul 2012
    Posts
    90

    Re: Can save images to a structured text file C# ?

    You need to convert the bitmap (Staff.Picture) to a byte[] before saving it to the file. See http://stackoverflow.com/questions/7...yte-array-in-c.

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

Featured