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.
Related
I have to create multiple thumbnails and save on amazon s3 in c#. I don't find library which can create thumbnail from memory stream only from source file. But can't have file source, I have only the stream from the upload and I don't want save localy the file.
Thx for ur help!
Simple c# code can do this for you. You can create a thumbnail from byte array.
First convert your memory stream object to byte array by following code
byte[] bytes = memoryStream.ToArray();
and then you can use below method
public byte[] MakeThumbnail(byte[] myImage, int thumbWidth, int thumbHeight)
{
using (MemoryStream ms = new MemoryStream())
using (Image thumbnail = Image.FromStream(new MemoryStream(myImage)).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
{
thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}
I don't find library which can create thumbnail from memory stream only from
source file
Really? Because I do not need a library for that - I can do that with standard C#.
System.Drawing.Bitmap has a constructor accepting a stream.
But can't have file source,
Actually you can - it is called a temporary file and there is a whole mechanism for that in .NET (and windows). Really.
But it is not needed. Read the manual, create a bitmap (by packing your byte array into a MemoryStream) and it should work.
I have copied some data in Clipboard using MS-Word Com API
Range.CopyAsPicture();
and when I am pasting(Ctrl + v) it on window's paint software its getting displayed.
Issue is while converting Clipboard data to image Using c#
I looked into various link on internet and tried following code which is not working
MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;
above line returning null
clipboardData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)
Above line returning false
Can anyone please suggest how can convert the clipboard data to image.
If all you are looking for is getting the bitmap of the image in the clipboard (or the underlying binary data), look into using GetImage.
Here is a code snippet you can try:
BitmapSource bmpSource = Clipboard.GetImage();
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
In case that also doesn't seem to work, you can try looking into this workaround, it doesn't seem directly related, but it might be just what you need.
Apparently MS Office puts images on the clipboard as a stream of PNG bytes, which is actually a really sensible innovation. This image data simply uses the identifier "PNG".
So try this instead:
if (retrievedData.GetDataPresent("PNG"))
{
MemoryStream png_stream = retrievedData.GetData("PNG") as MemoryStream;
if (png_stream != null)
return new Bitmap(png_stream);
}
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
I have a functioning application in c#/.net that currently accepts raw image data in a bayer format from a set of embedded cameras and converts them to jpeg images. To save transmission time, I have modified the embedded devices to encode the images as jpegs prior to transmission. I'm an experienced embedded programmer but a total c#/.net noob. I have managed to modify the application to save the arrays to file with a jpeg name using this snippet: ( the offset of 5 is to skip header data in the transmission frame)
FileStream stream = File.Create(fileName);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(multiBuff.msgData, 5, multiBuff.dataSize - 5);
writer.Close();
The files open up fine, but now I want to treat the data as a bitmap without having to save & load from file. I tried the following on the data array:
MemoryStream stream = new MemoryStream(data);
BinaryReader reader = new BinaryReader(stream);
byte[] headerData = reader.ReadBytes(5);
Bitmap bmpImage = new Bitmap(stream);
But this throws a parameter not valid exception. As a newbie, I'm a little overwhelmed with all the classes and methods for images and it seems like what I'm doing should be commonplace, but I can't find any examples in the usual places. Any ideas?
I think you are looking for Bitmap.FromStream() :
Bitmap bmpImage = (Bitmap)Bitmap.FromStream(stream);
Actually using new Bitmap(stream) should have worked as well - this means that the data in the stream does not constitute a valid image - are you sure the jpg is valid? Can you save it to disk and open it i.e. in Paint to test?
You use the Image class.
Image image;
using (MemoryStream stream = new MemoryStream(data))
{
image = Image.FromStream(stream);
}
FYI it didn't work because reader.ReadBytes(5) returns the 5 first bytes of stream not the bytes after position 5
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();
}