Reading GDAL Data from Tiff images Encoded as a Base64String - c#

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.

Related

How to convert a pdf bytes to an image byte

I have uploaded a PDF file and I convert it to byte format and save it in the database. On fetching the PDF byte array, I need to convert them to image format so that I can insert the image into a new PDF report
SqlCommand objCmd = new SqlCommand(sTSQL, con);
objCmd.CommandType = CommandType.Text;
object result = objCmd.ExecuteScalar();
byte[] byteArray;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, result);
byteArray = ms.ToArray(); // Byte Array
ms.Close();
ms = new MemoryStream(byteArray, 0, byteArray.Length);
ms.Seek(0, SeekOrigin.Begin);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);`enter code here`
Error Statement : "Parameter is not valid"
The problem is the stream that you're trying to write to an image is the byte representation of an actual PDF, so that won't work.
You can use tools such as ImageMagick - that is a .NET wrapper for the library which will allow you to convert a PDF to image. That will actually figure out what the PDF looks like when rendered and will turn it into an image.
ImageMagick is a powerful image manipulation library that supports
over 100 major file formats (not including sub-formats). With
Magick.NET you can use ImageMagick without having to install
ImageMagick on your server or desktop. Visit
https://github.com/dlemstra/Magick.NET/tree/master/Documentation
before installing to help you decide the best version.

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 split the header of an image?

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

How to read drawing image (.dwg) from byte array

I have one WPF application in which I am uploading autocad drawing files (.dwg), convert it to byte array and save to database. When I read back that file from byte array, I am getting following error :
No imaging component suitable to complete this operation was found.
My code to convert in byte array is below :
FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
I am trying to get image from byte array using below code :
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.CacheOption = BitmapCacheOption.Default;
bi.StreamSource = new MemoryStream(data);
RenderOptions.SetBitmapScalingMode(bi, BitmapScalingMode.Linear);
bi.EndInit();
Above code works fine for other image files like jpg, png, bmp, gif. but not working for dwg file. Can anybody guide me what's wrong in my code ?
Thanks
Times ago I was searching for DWG library in C# and found this one:
http://www.woutware.com/cadlib.html, but after never used it.
You can not threat DWG files like ordinar image files, DWG is fairly complicated format for storing 2D and 3D data as well. And years ago also was a subject of frequent change and all licensing mess.
Hope this helps.
You have answered it yourself!
Above code works fine for other image files like jpg, png, bmp, gif.
but not working for dwg file.
For this to work correctly deserialise the byte array back to as "dwg file" itself and use some APIs to convert it to a bitmap and use that as the image source.
Try using a type converter
FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
TypeConverter tc = TypeDescriptor.GetConverter(typeof(BitmapImage));
BitmapImage bitmap1 = (BitmapImage)tc.ConvertFrom(data);
Try using strm.Seek(0, SeekOrigin.Begin); because it may be possible you must have gone past through the stream with read
One option is to use http://www.opendwg.org/
Although not free, it is non-profit.
But it will only parse the dwg file for you into collection of lines, circles, polygons etc.
You still need to piece together and render the image.

how to convert a jpeg in an array into a bitmap

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

Categories