简体   繁体   中英

How to create image file from vfp filetostr() string in c#

Use vfp's filetostr() function to store the string returned by the image file into the text type field of sql server

So how to create a picture file after reading a string from sql server with C#?

What encoding is the string returned by filetostr and what type is it in C#

FileToStr() reads any file either binary or not. You never store a binary file to SQL server's text field. Text data type is depreceated anyway. You use Varbinary(MAX) instead. There is no special encoding returned with FileToStr(), it simply reads any file as is with no character encoding conversion. IOW, you can think it as ASCIIEncoding. In C# it is byte[] (not a char[]). If you look a file using a hex editor, you would see hex bytes, FileToStr() gets all those bytes as a single string (unlike C#, in VFP a string can contain any ASCII character including character 0x00).

You can simply get it as a byte[] and create Image using Image.FromStream(). ie:

void Main()
{
  byte[] mySavedPic; 
  using(SqlConnection connection = new SqlConnection(@"server=.\SQLExpress;Trusted_connection=yes;Database=ImageDb"))
  {
    SqlCommand cmd = new SqlCommand("select Picture from  myPictures where pictureId = 1",connection);
    connection.Open();
    mySavedPic = (byte[])cmd.ExecuteScalar();
    connection.Close();
  }
  
  
  Form f = new Form();
  PictureBox p = new PictureBox();
  p.SizeMode = PictureBoxSizeMode.StretchImage;
  p.Dock = DockStyle.Fill;
  
  using (MemoryStream ms = new MemoryStream(mySavedPic)) 
  {
      p.Image = Image.FromStream(ms);
  }

  f.Controls.Add( p );
  f.ShowDialog();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM