How to split the header of an image? - c#

I have designed a website which users upload some images and I store them in a folder.but anyone else can access the uploaded file via URL.
However I want to split the header of Uploaded images and insert the header in the database and store the rest of file in the folders.
How can I split the header of image?
If I convert the image to the array of binary how to distinguish the header part?

You can achieve that another way . First Encode the Bitmap to base64 and store it at an XML File ,keep XML Files if you want ordered by an ID and store within the XML 2 Items Value and BitmapString .Than from XML you can convert from Base64 to an Bitmap .
//Convert Image to Base64
Bitmap myBmp = new Bitmap(dialog.FileName);
MemoryStream theStream = new MemoryStream();
myBmp.Save(theStream, ImageFormat.Jpeg);
String base64Containter = Convert.ToBase64String(theStream.ToArray());
//Write String and Image ID to XML
Load image from XML assuming that you already identified the Item into XML:
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
char[] theBytes = base64Containter.ToArray();
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(Convert.FromBase64CharArray(theBytes, 0, theBytes.Length));
this.pictureBox1.Image = bitmap1;

you can make use of an image handler to retrieve the image from the db instead of splitting the image,
check this link which explains how to create an use an image handler

Related

Reading GDAL Data from Tiff images Encoded as a Base64String

I am currently trying to decode a (Geo)Tiff Image encoded as a Base64String back to the original format using C# so that I can access the GDAL Metadata as for example the Dataset.
Problem 1: I can't find any exact explanation on how to convert a Base64String encoded image to a tiff image. The method I found only returns null for tiff.
Tiff tiff;
String base64String = imageDataString.Replace("data:image/png;base64,", ""); // data:image/png;base64,
byte[] byteBuffer = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(byteBuffer);
tiff = Tiff.ClientOpen("in-memory", "r", ms, new TiffStream());
using BitMiracle.LibTiff.Classic.
Problem 2:
If the tiff image was decoded, how do I save it to the harddrive? This step seems to be necessary so that I can open the image to extract the GDAL Dataset as following:
Dataset GdalRoomSet = Gdal.Open(imagePath, 0);
double[] TransformationCoefficients = new double[6];
GdalRoomSet.GetGeoTransform(TransformationCoefficients);
Problem solved itselfe through jps' comment. The question is now obsolete, as I need a completely different approach.

How can I save a data:image/svg+xml;utf8 image as a valid image file using C#?

I am trying to download QR codes as images using C#. I know that I can't directly save the contents and that I need to collect the bytes, but how does one do that?
Here is what I have tried so far, but when opening the image it says its not a PNG?
var decodedUrl = WebUtility.UrlDecode(dataImage).Replace("data:image/svg xml;utf8,", "");
File.WriteAllBytes(_path + "/qr.png", Encoding.UTF8.GetBytes(decodedUrl));
How can I directly save this data:image link as a valid PNG or JPG?
data:image/svg+xml;utf8,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%0A%3Csvg%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20height%3D%22320%22%20version%3D%221.1%22%20viewBox%3D%220%200%20320%20320%22%20width%3D%22320%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%20%20%3Cpath%20d%3D%22M234.34%2C200.55c-26.8-12.97-30.99-32.94-31.23-34.49c-0.23-1.79-0.47-3.18%2C1.48-5.05c1.94-1.79%2C10.41-6.99%2C12.74-8.62c3.88-2.72%2C5.59-5.44%2C4.35-8.78c-0.85-2.33-3.03-3.18-5.28-3.18c-0.7%2C0-1.4%2C0.08-2.1%2C0.23c-4.27%2C0.93-8.39%2C3.11-10.8%2C3.65c-0.31%2C0.08-0.62%2C0.16-0.85%2C0.16c-1.24%2C0-1.71-0.54-1.63-2.1c0.31-4.66%2C0.93-13.83%2C0.23-22.29c-1.01-11.73-4.82-17.56-9.24-22.68c-2.17-2.49-12.2-13.13-31.62-13.13c-19.34%2C0-29.52%2C10.72-31.62%2C13.13C124.03%2C102.52%2C120.31%2C108.34%2C119.3%2C120.07c-0.7%2C8.54-0.08%2C17.63%2C0.23%2C22.29c0.08%2C1.48-0.39%2C2.1-1.63%2C2.1c-0.23%2C0-0.54%2C0-0.85-0.16c-2.41-0.54-6.52-2.72-10.8-3.65c-0.7-0.16-1.4-0.23-2.1-0.23c-2.25%2C0-4.43%2C0.85-5.28%2C3.18c-1.24%2C3.34%2C0.47%2C6.06%2C4.35%2C8.78c2.33%2C1.63%2C10.8%2C6.91%2C12.74%2C8.62c1.94%2C1.86%2C1.71%2C3.26%2C1.48%2C5.05c-0.16%2C1.48-4.43%2C21.52-31.23%2C34.49c-1.55%2C0.78-4.27%2C2.33%2C0.47%2C4.97c7.38%2C4.04%2C12.27%2C3.65%2C16.08%2C6.14c3.26%2C2.1%2C1.32%2C6.6%2C3.65%2C8.23c2.87%2C2.02%2C11.5-0.16%2C22.53%2C3.5c9.24%2C3.03%2C14.91%2C11.65%2C31.38%2C11.65c16.39%2C0%2C22.22-8.7%2C31.38-11.65c11.03-3.65%2C19.65-1.48%2C22.53-3.5c2.33-1.63%2C0.47-6.14%2C3.65-8.23c3.81-2.49%2C8.7-2.02%2C16.08-6.14C238.53%2C202.96%2C235.89%2C201.33%2C234.34%2C200.55M0%2C268.8C0%2C297.07%2C22.93%2C320%2C51.2%2C320L268.8%2C320C297.07%2C320%2C320%2C297.07%2C320%2C268.8L320%2C51.2C320%2C22.93%2C297.07%2C0%2C268.8%2C0L51.2%2C0C22.93%2C0%2C0%2C22.93%2C0%2C51.2L0%2C268.8%22%20fill%3D%22%23FFFC00%22%20fill-rule%3D%22evenodd%22%2F%3E%0A%20%20%3Cpath%20d%3D%22M245.99%2C199.07c-1.17-3.26-3.5-5.05-6.06-6.45c-0.47-0.31-0.93-0.54-1.32-0.7c-0.78-0.39-1.55-0.78-2.33-1.17c-8.16-4.27-14.45-9.71-18.88-16.16c-1.48-2.17-2.49-4.12-3.26-5.75c-0.39-1.09-0.39-1.71-0.08-2.25c0.23-0.39%2C0.78-0.85%2C1.01-1.09c1.4-0.93%2C2.87-1.86%2C3.81-2.49c1.71-1.17%2C3.11-2.02%2C4.04-2.64c3.34-2.33%2C5.67-4.82%2C7.15-7.53c2.02-3.88%2C2.33-8.39%2C0.7-12.51c-2.17-5.83-7.69-9.4-14.37-9.4c-1.4%2C0-2.8%2C0.16-4.19%2C0.47c-0.39%2C0.08-0.7%2C0.16-1.09%2C0.23c0.08-3.96%2C0-8.16-0.39-12.27c-1.24-14.53-6.37-22.14-11.65-28.2c-2.17-2.56-6.06-6.21-11.81-9.55C179.26%2C76.88%2C170.1%2C74.55%2C160.16%2C74.55c-9.94%2C0-19.03%2C2.33-27.03%2C6.91c-5.83%2C3.34-9.63%2C6.99-11.88%2C9.55c-5.28%2C6.06-10.41%2C13.67-11.65%2C28.2c-0.39%2C4.12-0.47%2C8.31-0.39%2C12.27c-0.39-0.08-0.7-0.16-1.09-0.23c-1.4-0.31-2.8-0.47-4.19-0.47c-6.68%2C0-12.12%2C3.57-14.37%2C9.4c-1.55%2C4.19-1.32%2C8.62%2C0.7%2C12.51c1.48%2C2.8%2C3.81%2C5.28%2C7.15%2C7.53c0.85%2C0.62%2C2.25%2C1.48%2C4.04%2C2.64c0.93%2C0.62%2C2.33%2C1.48%2C3.65%2C2.41c0.23%2C0.16%2C0.93%2C0.7%2C1.17%2C1.17c0.31%2C0.54%2C0.31%2C1.17-0.16%2C2.33c-0.7%2C1.55-1.71%2C3.5-3.18%2C5.59c-4.27%2C6.29-10.49%2C11.65-18.33%2C15.85c-4.19%2C2.25-8.47%2C3.65-10.33%2C8.62c-1.4%2C3.73-0.47%2C8%2C3.03%2C11.57l0%2C0c1.17%2C1.24%2C2.56%2C2.33%2C4.43%2C3.34c4.27%2C2.33%2C7.92%2C3.5%2C10.8%2C4.35c0.47%2C0.16%2C1.71%2C0.54%2C2.17%2C1.01c1.24%2C1.09%2C1.09%2C2.8%2C2.8%2C5.28c1.01%2C1.55%2C2.25%2C2.56%2C3.18%2C3.26c3.57%2C2.49%2C7.61%2C2.64%2C11.81%2C2.8c3.81%2C0.16%2C8.16%2C0.31%2C13.13%2C1.94c2.02%2C0.7%2C4.19%2C2.02%2C6.68%2C3.5c5.98%2C3.65%2C14.06%2C8.62%2C27.73%2C8.62c13.59%2C0%2C21.83-5.05%2C27.81-8.7c2.49-1.48%2C4.58-2.8%2C6.6-3.5c4.97-1.63%2C9.32-1.79%2C13.13-1.94c4.27-0.16%2C8.23-0.31%2C11.81-2.8c1.09-0.78%2C2.56-2.02%2C3.65-3.96c1.24-2.1%2C1.17-3.57%2C2.33-4.58c0.47-0.39%2C1.48-0.78%2C2.02-0.93c2.87-0.78%2C6.6-1.94%2C10.95-4.35c1.94-1.09%2C3.42-2.25%2C4.66-3.57c0%2C0%2C0%2C0%2C0.08-0.08C246.53%2C206.84%2C247.31%2C202.72%2C245.99%2C199.07%22%20fill%3D%22%23000000%22%2F%3E%0A%20%20%3Cpath%20d%3D%22M234.34%2C200.55c-26.8-12.97-30.99-32.94-31.23-34.49c-0.23-1.79-0.47-3.18%2C1.48-5.05c1.94-1.79%2C10.41-6.99%2C12.74-8.62c3.88-2.72%2C5.59-5.44%2C4.35-8.78c-0.85-2.33-3.03-3.18-5.28-3.18c-0.7%2C0-1.4%2C0.08-2.1%2C0.23c-4.27%2C0.93-8.39%2C3.11-10.8%2C3.65c-0.31%2C0.08-0.62%2C0.16-0.85%2C0.16c-1.24%2C0-1.71-0.54-1.63-2.1c0.31-4.66%2C0.93-13.83%2C0.23-22.29c-1.01-11.73-4.82-17.56-9.24-22.68c-2.17-2.49-12.2-13.13-31.62-13.13c-19.34%2C0-29.52%2C10.72-31.62%2C13.13C124.03%2C102.52%2C120.31%2C108.34%2C119.3%2C120.07c-0.7%2C8.54-0.08%2C17.63%2C0.23%2C22.29c0.08%2C1.48-0.39%2C2.1-1.63%2C2.1c-0.23%2C0-0.54%2C0-0.85-0.16c-2.41-0.54-6.52-2.72-10.8-3.65c-0.7-0.16-1.4-0.23-2.1-0.23c-2.25%2C0-4.43%2C0.85-5.28%2C3.18c-1.24%2C3.34%2C0.47%2C6.06%2C4.35%2C8.78c2.33%2C1.63%2C10.8%2C6.91%2C12.74%2C8.62c1.94%2C1.86%2C1.71%2C3.26%2C1.48%2C5.05c-0.16%2C1.48-4.43%2C21.52-31.23%2C34.49c-1.55%2C0.78-4.27%2C2.33%2C0.47%2C4.97c7.38%2C4.04%2C12.27%2C3.65%2C16.08%2C6.14c3.26%2C2.1%2C1.32%2C6.6%2C3.65%2C8.23c2.87%2C2.02%2C11.5-0.16%2C22.53%2C3.5c9.24%2C3.03%2C14.91%2C11.65%2C31.38%2C11.65c16.39%2C0%2C22.22-8.7%2C31.38-11.65c11.03-3.65%2C19.65-1.48%2C22.53-3.5c2.33-1.63%2C0.47-6.14%2C3.65-8.23c3.81-2.49%2C8.7-2.02%2C16.08-6.14C238.53%2C202.96%2C235.89%2C201.33%2C234.34%2C200.55%22%20fill%3D%22%23FFFFFF%22%2F%3E%0A%20%20%3Cpath%20d%3D%22M60%2C160C60%2C104.78%2C104.78%2C60%2C160%2C60L160%2C60C215.22%2C60%2C260%2C104.78%2C260%2C160L260%2C160C260%2C215.22%2C215.22%2C260%2C160%2C260L160%2C260C104.78%2C260%2C60%2C215.22%2C60%2C160L60%2C160M0%2C268.8C0%2C297.07%2C22.93%2C320%2C51.2%2C320L268.8%2C320C297.07%2C320%2C320%2C297.07%2C320%2C268.8L320%2C51.2C320%2C22.93%2C297.07%2C0%2C268.8%2C0L51.2%2C0C22.93%2C0%2C0%2C22.93%2C0%2C51.2L0%2C268.8%22%20fill%3D%22%23000000%22%2F%3E%0A%20%20%3Cpath%20d%3D%22M10%2C51.2C10%2C28.45%2C28.45%2C10%2C51.2%2C10L268.8%2C10C291.55%2C10%2C310%2C28.45%2C310%2C51.2L310%2C268.8C310%2C291.55%2C291.55%2C310%2C268.8%2C310L51.2%2C310C28.45%2C310%2C10%2C291.55%2C10%2C268.8L10%2C51.2M53.16%2C17.63A5%2C5%2C0%2C0%2C0%2C53.16%2C27.63A5%2C5%2C0%2C0%2C0%2C53.16%2C17.63M68.42%2C17.63A5%2C5%2C0%2C0%2C0%2C68.42%2C27.63A5%2C5%2C0%2C0%2C0%2C68.42%2C17.63M98.95%2C17.63A5%2C5%2C0%2C0%2C0%2C98.95%2C27.63A5%2C5%2C0%2C0%2C0%2C98.95%2C17.63M175.26%2C17.63A5%2C5%2C0%2C0%2C0%2C175.26%2C27.63A5%2C5%2C0%2C0%2C0%2C175.26%2C17.63M190.53%2C17.63A5%2C5%2C0%2C0%2C0%2C190.53%2C27.63A5%2C5%2C0%2C0%2C0%2C190.53%2C17.63M205.79%2C17.63A5%2C5%2C0%2C0%2C0%2C205.79%2C27.63A5%2C5%2C0%2C0%2C0%2C205.79%2C17.63M221.05%2C17.63A5%2C5%2C0%2C0%2C0%2C221.05%2C27.63A5%2C5%2C0%2C0%2C0%2C221.05%2C17.63M266.84%2C17.63A5%2C5%2C0%2C0%2C0%2C266.84%2C27.63A5%2C5%2C0%2C0%2C0%2C266.84%2C17.63M37.89%2C32.89A5%2C5%2C0%2C0%2C0%2C37.89%2C42.89A5%2C5%2C0%2C0%2C0%2C37.89%2C32.89M68.42%2C32.89A5%2C5%2C0%2C0%2C0%2C68.42%2C42.89A5%2C5%2C0%2C0%2C0%2C68.42%2C32.89M83.68%2C32.89A5%2C5%2C0%2C0%2C0%2C83.68%2C42.89A5%2C5%2C0%2C0%2C0%2C83.68%2C32.89M129.47%2C32.89A5%2C5%2C0%2C0%2C0%2C129.47%2C42.89A5%2C5%2C0%2C0%2C0%2C129.47%2C32.89M160%2C32.89A5%2C5%2C0%2C0%2C0%2C160%2C42.89A5%2C5%2C0%2C0%2C0%2C160%2C32.89M175.26%2C32.89A5%2C5%2C0%2C0%2C0%2C175.26%2C42.89A5%2C5%2C0%2C0%2C0%2C175.26%2C32.89M190.53%2C32.89A5%2C5%2C0%2C0%2C0%2C190.53%2C42.89A5%2C5%2C0%2C0%2C0%2C190.53%2C32.89M205.79%2C32.89A5%2C5%2C0%2C0%2C0%2C205.79%2C42.89A5%2C5%2C0%2C0%2C0%2C205.79%2C32.89M221.05%2C32.89A5%2C5%2C0%2C0%2C0%2C221.05%2C42.89A5%2C5%2C0%2C0%2C0%2C221.05%2C32.89M53.16%2C48.16A5%2C5%2C0%2C0%2C0%2C53.16%2C58.16A5%2C5%2C0%2C0%2C0%2C53.16%2C48.16M68.42%2C48.16A5%2C5%2C0%2C0%2C0%2C68.42%2C58.16A5%2C5%2C0%2C0%2C0%2C68.42%2C48.16M129.47%2C48.16A5%2C5%2C0%2C0%2C0%2C129.47%2C58.16A5%2C5%2C0%2C0%2C0%2C129.47%2C48.16M160%2C48.16A5%2C5%2C0%2C0%2C0%2C160%2C58.16A5%2C5%2C0%2C0%2C0%2C160%2C48.16M175.26%2C48.16A5%2C5%2C0%2C0%2C0%2C175.26%2C58.16A5%2C5%2C0%2C0%2C0%2C175.26%2C48.16M221.05%2C48.16A5%2C5%2C0%2C0%2C0%2C221.05%2C58.16A5%2C5%2C0%2C0%2C0%2C221.05%2C48.16M236.32%2C48.16A5%2C5%2C0%2C0%2C0%2C236.32%2C58.16A5%2C5%2C0%2C0%2C0%2C236.32%2C48.16M251.58%2C48.16A5%2C5%2C0%2C0%2C0%2C251.58%2C58.16A5%2C5%2C0%2C0%2C0%2C251.58%2C48.16M37.89%2C63.42A5%2C5%2C0%2C0%2C0%2C37.89%2C73.42A5%2C5%2C0%2C0%2C0%2C37.89%2C63.42M68.42%2C63.42A5%2C5%2C0%2C0%2C0%2C68.42%2C73.42A5%2C5%2C0%2C0%2C0%2C68.42%2C63.42M98.95%2C63.42A5%2C5%2C0%2C0%2C0%2C98.95%2C73.42A5%2C5%2C0%2C0%2C0%2C98.95%2C63.42M236.32%2C63.42A5%2C5%2C0%2C0%2C0%2C236.32%2C73.42A5%2C5%2C0%2C0%2C0%2C236.32%2C63.42M22.63%2C78.68A5%2C5%2C0%2C0%2C0%2C22.63%2C88.68A5%2C5%2C0%2C0%2C0%2C22.63%2C78.68M37.89%2C78.68A5%2C5%2C0%2C0%2C0%2C37.89%2C88.68A5%2C5%2C0%2C0%2C0%2C37.89%2C78.68M53.16%2C78.68A5%2C5%2C0%2C0%2C0%2C53.16%2C88.68A5%2C5%2C0%2C0%2C0%2C53.16%2C78.68M68.42%2C78.68A5%2C5%2C0%2C0%2C0%2C68.42%2C88.68A5%2C5%2C0%2C0%2C0%2C68.42%2C78.68M83.68%2C78.68A5%2C5%2C0%2C0%2C0%2C83.68%2C88.68A5%2C5%2C0%2C0%2C0%2C83.68%2C78.68M236.32%2C78.68A5%2C5%2C0%2C0%2C0%2C236.32%2C88.68A5%2C5%2C0%2C0%2C0%2C236.32%2C78.68M297.37%2C78.68A5%2C5%2C0%2C0%2C0%2C297.37%2C88.68A5%2C5%2C0%2C0%2C0%2C297.37%2C78.68M37.89%2C93.95A5%2C5%2C0%2C0%2C0%2C37.89%2C103.95A5%2C5%2C0%2C0%2C0%2C37.89%2C93.95M53.16%2C93.95A5%2C5%2C0%2C0%2C0%2C53.16%2C103.95A5%2C5%2C0%2C0%2C0%2C53.16%2C93.95M68.42%2C93.95A5%2C5%2C0%2C0%2C0%2C68.42%2C103.95A5%2C5%2C0%2C0%2C0%2C68.42%2C93.95M282.11%2C93.95A5%2C5%2C0%2C0%2C0%2C282.11%2C103.95A5%2C5%2C0%2C0%2C0%2C282.11%2C93.95M297.37%2C109.21A5%2C5%2C0%2C0%2C0%2C297.37%2C119.21A5%2C5%2C0%2C0%2C0%2C297.37%2C109.21M22.63%2C124.47A5%2C5%2C0%2C0%2C0%2C22.63%2C134.47A5%2C5%2C0%2C0%2C0%2C22.63%2C124.47M53.16%2C124.47A5%2C5%2C0%2C0%2C0%2C53.16%2C134.47A5%2C5%2C0%2C0%2C0%2C53.16%2C124.47M266.84%2C124.47A5%2C5%2C0%2C0%2C0%2C266.84%2C134.47A5%2C5%2C0%2C0%2C0%2C266.84%2C124.47M297.37%2C124.47A5%2C5%2C0%2C0%2C0%2C297.37%2C134.47A5%2C5%2C0%2C0%2C0%2C297.37%2C124.47M297.37%2C155A5%2C5%2C0%2C0%2C0%2C297.37%2C165A5%2C5%2C0%2C0%2C0%2C297.37%2C155M37.89%2C170.26A5%2C5%2C0%2C0%2C0%2C37.89%2C180.26A5%2C5%2C0%2C0%2C0%2C37.89%2C170.26M282.11%2C170.26A5%2C5%2C0%2C0%2C0%2C282.11%2C180.26A5%2C5%2C0%2C0%2C0%2C282.11%2C170.26M297.37%2C170.26A5%2C5%2C0%2C0%2C0%2C297.37%2C180.26A5%2C5%2C0%2C0%2C0%2C297.37%2C170.26M22.63%2C185.53A5%2C5%2C0%2C0%2C0%2C22.63%2C195.53A5%2C5%2C0%2C0%2C0%2C22.63%2C185.53M53.16%2C185.53A5%2C5%2C0%2C0%2C0%2C53.16%2C195.53A5%2C5%2C0%2C0%2C0%2C53.16%2C185.53M266.84%2C185.53A5%2C5%2C0%2C0%2C0%2C266.84%2C195.53A5%2C5%2C0%2C0%2C0%2C266.84%2C185.53M282.11%2C185.53A5%2C5%2C0%2C0%2C0%2C282.11%2C195.53A5%2C5%2C0%2C0%2C0%2C282.11%2C185.53M22.63%2C200.79A5%2C5%2C0%2C0%2C0%2C22.63%2C210.79A5%2C5%2C0%2C0%2C0%2C22.63%2C200.79M37.89%2C200.79A5%2C5%2C0%2C0%2C0%2C37.89%2C210.79A5%2C5%2C0%2C0%2C0%2C37.89%2C200.79M266.84%2C200.79A5%2C5%2C0%2C0%2C0%2C266.84%2C210.79A5%2C5%2C0%2C0%2C0%2C266.84%2C200.79M282.11%2C200.79A5%2C5%2C0%2C0%2C0%2C282.11%2C210.79A5%2C5%2C0%2C0%2C0%2C282.11%2C200.79M297.37%2C200.79A5%2C5%2C0%2C0%2C0%2C297.37%2C210.79A5%2C5%2C0%2C0%2C0%2C297.37%2C200.79M68.42%2C216.05A5%2C5%2C0%2C0%2C0%2C68.42%2C226.05A5%2C5%2C0%2C0%2C0%2C68.42%2C216.05M251.58%2C216.05A5%2C5%2C0%2C0%2C0%2C251.58%2C226.05A5%2C5%2C0%2C0%2C0%2C251.58%2C216.05M282.11%2C216.05A5%2C5%2C0%2C0%2C0%2C282.11%2C226.05A5%2C5%2C0%2C0%2C0%2C282.11%2C216.05M68.42%2C231.32A5%2C5%2C0%2C0%2C0%2C68.42%2C241.32A5%2C5%2C0%2C0%2C0%2C68.42%2C231.32M266.84%2C231.32A5%2C5%2C0%2C0%2C0%2C266.84%2C241.32A5%2C5%2C0%2C0%2C0%2C266.84%2C231.32M282.11%2C231.32A5%2C5%2C0%2C0%2C0%2C282.11%2C241.32A5%2C5%2C0%2C0%2C0%2C282.11%2C231.32M297.37%2C231.32A5%2C5%2C0%2C0%2C0%2C297.37%2C241.32A5%2C5%2C0%2C0%2C0%2C297.37%2C231.32M53.16%2C246.58A5%2C5%2C0%2C0%2C0%2C53.16%2C256.58A5%2C5%2C0%2C0%2C0%2C53.16%2C246.58M98.95%2C246.58A5%2C5%2C0%2C0%2C0%2C98.95%2C256.58A5%2C5%2C0%2C0%2C0%2C98.95%2C246.58M221.05%2C246.58A5%2C5%2C0%2C0%2C0%2C221.05%2C256.58A5%2C5%2C0%2C0%2C0%2C221.05%2C246.58M236.32%2C246.58A5%2C5%2C0%2C0%2C0%2C236.32%2C256.58A5%2C5%2C0%2C0%2C0%2C236.32%2C246.58M251.58%2C246.58A5%2C5%2C0%2C0%2C0%2C251.58%2C256.58A5%2C5%2C0%2C0%2C0%2C251.58%2C246.58M297.37%2C246.58A5%2C5%2C0%2C0%2C0%2C297.37%2C256.58A5%2C5%2C0%2C0%2C0%2C297.37%2C246.58M22.63%2C261.84A5%2C5%2C0%2C0%2C0%2C22.63%2C271.84A5%2C5%2C0%2C0%2C0%2C22.63%2C261.84M53.16%2C261.84A5%2C5%2C0%2C0%2C0%2C53.16%2C271.84A5%2C5%2C0%2C0%2C0%2C53.16%2C261.84M68.42%2C261.84A5%2C5%2C0%2C0%2C0%2C68.42%2C271.84A5%2C5%2C0%2C0%2C0%2C68.42%2C261.84M98.95%2C261.84A5%2C5%2C0%2C0%2C0%2C98.95%2C271.84A5%2C5%2C0%2C0%2C0%2C98.95%2C261.84M114.21%2C261.84A5%2C5%2C0%2C0%2C0%2C114.21%2C271.84A5%2C5%2C0%2C0%2C0%2C114.21%2C261.84M129.47%2C261.84A5%2C5%2C0%2C0%2C0%2C129.47%2C271.84A5%2C5%2C0%2C0%2C0%2C129.47%2C261.84M144.74%2C261.84A5%2C5%2C0%2C0%2C0%2C144.74%2C271.84A5%2C5%2C0%2C0%2C0%2C144.74%2C261.84M160%2C261.84A5%2C5%2C0%2C0%2C0%2C160%2C271.84A5%2C5%2C0%2C0%2C0%2C160%2C261.84M175.26%2C261.84A5%2C5%2C0%2C0%2C0%2C175.26%2C271.84A5%2C5%2C0%2C0%2C0%2C175.26%2C261.84M190.53%2C261.84A5%2C5%2C0%2C0%2C0%2C190.53%2C271.84A5%2C5%2C0%2C0%2C0%2C190.53%2C261.84M221.05%2C261.84A5%2C5%2C0%2C0%2C0%2C221.05%2C271.84A5%2C5%2C0%2C0%2C0%2C221.05%2C261.84M251.58%2C261.84A5%2C5%2C0%2C0%2C0%2C251.58%2C271.84A5%2C5%2C0%2C0%2C0%2C251.58%2C261.84M282.11%2C261.84A5%2C5%2C0%2C0%2C0%2C282.11%2C271.84A5%2C5%2C0%2C0%2C0%2C282.11%2C261.84M37.89%2C277.11A5%2C5%2C0%2C0%2C0%2C37.89%2C287.11A5%2C5%2C0%2C0%2C0%2C37.89%2C277.11M83.68%2C277.11A5%2C5%2C0%2C0%2C0%2C83.68%2C287.11A5%2C5%2C0%2C0%2C0%2C83.68%2C277.11M98.95%2C277.11A5%2C5%2C0%2C0%2C0%2C98.95%2C287.11A5%2C5%2C0%2C0%2C0%2C98.95%2C277.11M129.47%2C277.11A5%2C5%2C0%2C0%2C0%2C129.47%2C287.11A5%2C5%2C0%2C0%2C0%2C129.47%2C277.11M205.79%2C277.11A5%2C5%2C0%2C0%2C0%2C205.79%2C287.11A5%2C5%2C0%2C0%2C0%2C205.79%2C277.11M221.05%2C277.11A5%2C5%2C0%2C0%2C0%2C221.05%2C287.11A5%2C5%2C0%2C0%2C0%2C221.05%2C277.11M236.32%2C277.11A5%2C5%2C0%2C0%2C0%2C236.32%2C287.11A5%2C5%2C0%2C0%2C0%2C236.32%2C277.11M266.84%2C277.11A5%2C5%2C0%2C0%2C0%2C266.84%2C287.11A5%2C5%2C0%2C0%2C0%2C266.84%2C277.11M53.16%2C292.37A5%2C5%2C0%2C0%2C0%2C53.16%2C302.37A5%2C5%2C0%2C0%2C0%2C53.16%2C292.37M68.42%2C292.37A5%2C5%2C0%2C0%2C0%2C68.42%2C302.37A5%2C5%2C0%2C0%2C0%2C68.42%2C292.37M98.95%2C292.37A5%2C5%2C0%2C0%2C0%2C98.95%2C302.37A5%2C5%2C0%2C0%2C0%2C98.95%2C292.37M114.21%2C292.37A5%2C5%2C0%2C0%2C0%2C114.21%2C302.37A5%2C5%2C0%2C0%2C0%2C114.21%2C292.37M129.47%2C292.37A5%2C5%2C0%2C0%2C0%2C129.47%2C302.37A5%2C5%2C0%2C0%2C0%2C129.47%2C292.37M144.74%2C292.37A5%2C5%2C0%2C0%2C0%2C144.74%2C302.37A5%2C5%2C0%2C0%2C0%2C144.74%2C292.37M160%2C292.37A5%2C5%2C0%2C0%2C0%2C160%2C302.37A5%2C5%2C0%2C0%2C0%2C160%2C292.37M175.26%2C292.37A5%2C5%2C0%2C0%2C0%2C175.26%2C302.37A5%2C5%2C0%2C0%2C0%2C175.26%2C292.37M221.05%2C292.37A5%2C5%2C0%2C0%2C0%2C221.05%2C302.37A5%2C5%2C0%2C0%2C0%2C221.05%2C292.37M236.32%2C292.37A5%2C5%2C0%2C0%2C0%2C236.32%2C302.37A5%2C5%2C0%2C0%2C0%2C236.32%2C292.37M266.84%2C292.37A5%2C5%2C0%2C0%2C0%2C266.84%2C302.37A5%2C5%2C0%2C0%2C0%2C266.84%2C292.37M62%2C160C62%2C214.12%2C105.88%2C258%2C160%2C258L160%2C258C214.12%2C258%2C258%2C214.12%2C258%2C160L258%2C160C258%2C105.88%2C214.12%2C62%2C160%2C62L160%2C62C105.88%2C62%2C62%2C105.88%2C62%2C160L62%2C160%22%20fill%3D%22%23FFFC00%22%2F%3E%0A%3C%2Fsvg%3E%0A

Error in converting jpg to gif using Image.Save

What i am trying to do is to convert an image into a byte array and then write that byte array into a file. here's the code
public static byte[] Convert(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
// or whatever output format you like
return ms.ToArray();
}
}
public Form1()
{
InitializeComponent();
Bitmap pic = new Bitmap("tulips.jpg");
pictureBox1.Image = pic;
byte[] img_array;
img_array = Convert(pic);
File.WriteAllBytes("test.txt", img_array);
}
Now I have been successfully able to convert the image into byte array. I have checked the values in byte array by means of a breakpoint and all of them are valid.
However when I try to write the array into a file and then open the file all I see is garbage.
Am I missing something?
Your Bitmap is an Image. Why do you convert it into a byte array when you can simply call Save (documented here)
Bitmap pic = new Bitmap("tulips.jpg");
pictureBox1.Image = pic;
pic.Save("test.gif", System.Drawing.Imaging.ImageFormat.Gif);
To manipulate the image, you will usually access the pixel data - what you have now contains the file type header as well as the pixels! See Bitmap.LockBits to manipulate the pixels (documented here)
Are you sure that you are trying to open the file with an image viewer? The .txt extension would typically cause it to be opened with a text editor instead. Since the format of an image file is binary, it is to be expected that you would only see "garbage" when you render it as text. It would help if you use the correct extension, .gif, when saving the file.
File.WriteAllBytes("test.gif", img_array);
What did you expect to see? You're saving the byte array to a text file. When you open a text file that has image file bytes, there's no way you're going to get a good result.
Ensure that you're saving the file as the proper type, then see what happens.

How to store an image in a SQL Server database from Silverlight app?

I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:
How to convert an image to binary from a url to store it into my database (store all the image and not only the url)
Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)
Finally, when the image is stored, how to read it from Silverlight?
Thank you in advance .
You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.
System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;
// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)
To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).
Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/
Add bytes from memory stream into database
Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx
It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

Convert BMP to PNG in memory for Clipboard pasting in .Net

This similar question's answers all require the file to be saved. However, I'm trying to convert the file and then copy it to the clipboard.
How can I convert a Bitmap (or any image) to a PNG without saving it to the file system?
Update:
I'm trying to paste the image into an application (in this case Evernote). When you copy an image into the clipboard (e.g. via the browser), it remembers its image format and when you paste it in, it will create an image with the same exact format. For example, if you copy a PNG, it will paste a PNG. If you copy a JPG, it will paste a JPG, etc.
I am trying to take whatever image is currently in the clipboard, scale it to the size I want, and then keep it in the clipboard as a PNG, such that when it is pasted into Evernote, it will create a PNG.
When I copy a PNG image in my browser, I see the following formats: HTML FORMAT, CF_BITMAP, CF_DIB, CF_DIBV5. I'm not sure which of these Evernote is using for pasting. I was under the impression that it was CF_BITMAP, but after reading the comments below, I guess it's using one of the other formats.
How can I place an image in the clipboard which will be treated as a PNG when pasted?
Save the Bitmap to a MemoryStream
byte[] result = null;
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
result = stream.ToArray();
}

Categories