I need to extract tiles from a tif image and write them in jpg format.
Based on a previous search, I wrote some C# code that works in a C# WinForm project on my desktop. Then I had the idea to put it as a WebService but i cannot do it, because this code is using Marshal.Copy
that doesn’t work on the remote server (despite I have there the full trust
mode. I have read some comment saying that this could be the problem, but it isn’t, in my case).
The source image is Tif
, therefore I have read the part of the image corresponding to the tile, in a buffer named output
. If nothing better is possible, I could accept to set the resulting image Pixel by Pixel, but I don’t see how to do it in C#. In other words I am not able to set the pixels in the resulting image, using the bytes[] that I extracted from the Tif
.
The code which works on the desktop is:
using (var image = new Bitmap(W, H, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
var bmpData = image.LockBits(new Rectangle(0, 0, W, H),ImageLockMode.WriteOnly,
Image.PixelFormat);
var ptr = bmpData.Scan0;
Marshal.Copy(output, 0, ptr, output.Length);
image.UnlockBits(bmpData);
image.Save(origin + "\" + TileName, ImageFormat.Png)
}
2
Answers
By the way, @JuanR suggestion allowed me to resolve the problem. The following code works, despite it gives me an out of memory error, after creating some tiles. This latter error should not be related to my original problem. I took the code from Microsoft documentation. With my surprise, despite it was described to use a decoder, I commented it out and used directly a bitmap, as you can see in the following:
Final version without Out of Memory: