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

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[]) - same as doing a File.ReadAllBytes()-. 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();
}

Related

QnA Bot Framework - How to do accents like "á"

In my qna maker knowledge base I have this:
Question:
Hello
Answer:
Hello maría
But I got this answer on the bot: Hello mar&#237a . I tried many things and there is no results.
Thanks.
you can use the below code where you are getting your response from QNA and pass it to code.
static void Main()
{
string unicodeString = "This string contains the unicode character Pi (\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte array.
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
do let me know in case you need more help
I have created a sample here:
https://github.com/FranciscoPonceGomez/FranciscoQnAccents
It works in all channels for me. You should not have any problems with accents.
Knowledge Base:
You can try it here:
https://franciscoqnaccents.azurewebsites.net/
Let me know if there is anything in the code that doesn't make sense to you.
Regards,
Francisco

C# and ColdFusion AES Encryption not matching

I have to encrypt url query sting in C# and pass to ColdFusion page. Can someone help me on writing encryption code using AES algorithm in C#.net that is equivalent to below ColdFusion function? Thanks in advance.
<cfset strLink = Encrypt("top secret", "WTq8zYcZfaWVvMncigHqwQ==", "AES","Hex")>
CF Result:
strLink = 91E72250B8A7EDBC4E5AF37F04E6AB5B
I tried below code in C#, but the results are not matching.
byte[] plainText = Encoding.Unicode.GetBytes("top secret");
byte[] key = Convert.FromBase64String("WTq8zYcZfaWVvMncigHqwQ==");
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.PKCS7;
algorithm.BlockSize = 128;
algorithm.KeySize = 128;
algorithm.Key = key;
string result;
using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
result = Convert.ToBase64String(memoryStream.ToArray());
}
}
}
return result;
C# Result:
HEX = 89F9F3C55CD232362FE1E14240C479BE5B56210FF3913E7B6BA4BCD3C87F9AA7
Base64 = ifnzxVzSMjYv4eFCQMR5vltWIQ/zkT57a6S808h/mqc=
(From comments...)
This is a perfect example of how character encoding makes a big difference.
Believe it or not, it is simply due to using the wrong encoding in the C# code. Encoding.Unicode uses UTF-16, whereas CF's Encrypt function always uses UTF-8 (very different). Consequently, the C# code is encrypting a totally different value than CF. Hence the different results, and why the length of the C# string (hex) is longer than the one returned from CF.
Use Encoding.UTF8.GetBytes() instead of Encoding.Unicode.GetBytes() and the results will match:
byte[] plainText = Encoding.UTF8.GetBytes("top secret");

Convert IsolatedStorageFileStream to b64 string in wp7

I have mp4 files in Isolated Storage and I am reading this by using IsolatedStorageFileStream. After read the file I need to convert it into b64 string.
So I am not getting proper code for this. If anyone knows then please help me.
//Convert the IsolatedStorageFileStream to Base64 String and take the length of the file.
BinaryReader reader = new BinaryReader(isoVideoFile);
long length = reader.BaseStream.Length;
int rr = Convert.ToInt32(length);
byte[] chunk = reader.ReadBytes(rr);//reading the bytes
string temp_inBase64 = Convert.ToBase64String(chunk);
You can use Convert.ToBase64String(bytes[])

How I can correctly read txt file in windows phone?

I try to read txt file with next content:
I using this method to read txt file:
public string ReadFileContents()
{
//this verse is loaded for the first time so fill it from the text file
var ResrouceStream = Application.GetResourceStream(new Uri("Files/info.txt", UriKind.Relative));
if (ResrouceStream != null)
{
Stream myFileStream = ResrouceStream.Stream;
if (myFileStream.CanRead)
{
StreamReader myStreamReader = new StreamReader(myFileStream);
//read the content here
return myStreamReader.ReadToEnd();
}
}
return string.Empty;
}
This method return to me next string with wrong symbols:
How I can correctly read txt file??
How are you showing the text? Maybe it's the printing code rather than the reading code.
Also, is the BOM correct on the file? I believe the first 3 bytes specify the encoding type. Are they correct for this encoding?
Wrong symbols: When reading an ANSI encoded text-file on Windows Phone will cause umlauts, special characters etc. "looking wrong" since you have to use UTF-8 on Windows Phone.

MVC3 Download Excel Workbook from CSV data

I've got a set of data that exists in memory in a CSV format. I have this method in my controller:
public FileContentResult ItemsAsExcelExport(){
var model = _itemService.GetExcelExportModel();
return new FileContentResult(model.CSVData, model.MimeType){FileDownloadName = model.FileName};
}
The problem here is that my model.CSVData property returns a simple comma delimited set of values. I'm not sure how I can satisfy the fileContents argument of the FileContentResult contructor. It's asking for a byte array.
Thanks in advance.
Take a look at this question How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
The solution is
byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);

Resources