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?
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
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.
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?
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.