Convert a variable to a byte array - windows-phone-7

I want to convert a video into a byte array for upload.
var videoInput = new Uri("/uploadNewVideo;component/test.mp4", UriKind.Relative);
I am trying to upload the byte array as parameter to php:
string a = "TRIAL";
string url = "phpURL?";
client.DownloadStringAsync(new Uri(url + "&uploadedfile=" + bytearray + "&name=" + a));

First of all, you are using the wrong method to download the video itself. Instead of DownloadStringAsync, you need to use OpenReadAsync. That way you can access the incoming byte stream.
Also, FYI, you will not be able to concatenate a byte array and a string the way you do it.

Related

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[]) - 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();
}

Get TIFF tag value (including non-ASCII characters) from TIFF images in Java 11

I am trying to read different tag values (like tags 259 (Compression), 33432 (Copyright), 306 (DateTime), 315 (Artist) etc.) from a TIFF image in Java. Can anyone suggest what is best way to get those values in Java 11 ?
i tried to get those values using tiffinfo commands (like "tiffinfo -c myfile.tif"). But i did not find any specific command in tiffinfo (libtiff) or any Java library which will give me the specific tag values (e.g. DateTime) of a TIFF image.
Update:
As haraldK suggested, i tried with ImageIO like following
try (ImageInputStream input = ImageIO.createImageInputStream(tiffFile)) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle reader not found
reader.setInput(input);
IIOMetadata metadata = reader.getImageMetadata(0);
TIFFDirectory ifd = TIFFDirectory.createFromMetadata​(metadata);
TIFFField dateTime = ifd.get​TIFFField(306);
String dateString = dateTime.getAsString(0);
}
But it does not give exact value of the tag. In case of non-ASCII value (ö, ü, ä etc), question marks replace the real values.
Can anyone tell me how to get the exact value (including non-ASCII) of the tag from TIFFField ?
You can use standard ImageIO, read the TIFF image metadata and get the requested values from it, like this by using some extra support classes in the JDK, starting from Java 9:
try (ImageInputStream input = ImageIO.createImageInputStream(tiffFile)) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle reader not found
reader.setInput(input);
IIOMetadata metadata = reader.getImageMetadata(0); // 0 is the index of first image
TIFFDirectory ifd = TIFFDirectory.createFromMetadata​(metadata);
TIFFField dateTime = ifd.get​TIFFField(306); // Yes, that's 3 F's...
String dateString = dateTime.getAsString(0); // TIFF dates are strings...
}
tiffFile must be a valid (existing, readable) java.io.File, java.io.RandomAccessFile or java.io.InputStream (or other supported input, this is plugin-based, really). If not, input will be null, and the code will fail.
You can use similar, but a lot more verbose version, that will work in older versions of Java, as long as you have a TIFF plugin:
try (ImageInputStream input = ImageIO.createImageInputStream(tiffFile)) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle reader not found
reader.setInput(input);
IIOMetadata metadata = reader.getImageMetadata(0); // 0 is the index of first image
// Get "native" TIFF metadata for first IFD
IIOMetadataNode root = metadata.getAsTree("com_sun_media_imageio_plugins_tiff_image_1.0");
Node ifd = root.getFirstChild();
NodeList fields = ifd.getElementsByTagName("TIFFField"); // Yes, that's 3 F's...
for (int i = 0; i < fields.getLength(); i++) {
Element field = (Element) fields.item(i);
if ("306".equals(field.getAttribute("number"))) {
// This is your DateTime (306) tag,
// now do something with it 😀
// ...
}
}
}
Hardly elegant code, though... The Java 9+ approach is much cleaner and easier to reason about.

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

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[])

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