On my web project I use the code below to get the bytes of an image.
public byte[] imageToByteArray(Image image)
{
using (var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
When I debug my web application by Visual Studio, it starts the IIS Express (http://localhost:13153/) and the bytes obtained of the above method matches the image.
But, when I run my web application by IIS 8, directly on the browser "http://localhost/lab/testproject", the method return a completly wrong data.
Here is the results:
Debug by visual studio:
137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,200,0,0,0,100,8,6,0,0,0,195,134,127,11,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0,0,9,112,72,89,115,0,0,14,195,0,0,14,195,1,199,111,168,100,0,0,3,95,73,68,65,84,120,94,237,217,141,113,226,58,24,5,208,20,69,71,174,135,114,210,76,154,241,195,128,228,255,107,224,129,55,187,62,103,70,51,49,193,146,62,164,139,237,228,171,5,86,9,8,4,2,2,129,128,64,32,32,16,8,8,4,2,2,129,128,64,32,32,16,8,8,4,2,2,129,128,64,32,32,16,8,8,4,2,2,129,128,64,32,32,16,8,8,4,2,2,193,195,1,249,57,159,218,175,175,175,216,78,231,159,251,187,63,233,167,61,159,186,241,78,237,46,195,237,238,95,175,239,239,242,92,64,154,239,219,193,119,83,67,209,191,244,198,128,252,156,219,243,189,223,185,63,176,129,226,124,94,240,219,234,99,213,19,1,105,250,5,91,8,72,183,232,205,91,86,244,182,65,106,191,127,220,187,231,243,219,234,35,121,237,25,100,41,32,111,210,93,137,62,209,239,171,222,61,159,223,86,31,217,199,2,50,126,102,105,218,225,219,102,207,51,247,78,202,230,233,219,248,188,206,240,220,50,246,90,127,139,6,115,191,181,249,24,69,154,207,94,245,141,143,191,219,230,254,243,245,118,246,114,213,62,13,143,139,173,26,103,191,191,183,211,249,114,125,187,73,245,29,201,71,2,82,62,220,242,187,235,198,40,31,126,89,212,225,113,93,152,114,255,189,220,239,77,191,73,174,239,137,253,77,149,115,111,11,94,55,236,234,96,203,243,217,181,190,193,57,215,121,151,254,187,214,245,91,214,162,142,177,81,99,61,255,190,233,203,249,131,9,197,250,14,230,3,1,25,47,80,103,252,129,247,27,96,254,161,191,178,129,82,127,89,253,150,92,29,108,105,62,123,215,55,57,167,110,240,251,67,124,93,139,126,62,67,211,26,235,113,153,219,236,252,173,250,142,229,253,1,25,126,195,77,90,127,27,48,216,68,151,214,247,241,202,6,234,172,245,183,166,31,231,218,86,79,88,152,207,238,245,189,26,144,149,26,215,174,32,37,48,15,213,119,28,31,188,130,60,240,103,202,218,79,121,239,43,27,104,96,214,223,220,237,150,227,246,251,255,119,5,217,171,190,231,3,178,89,227,52,4,163,201,60,81,223,1,124,228,25,164,222,247,14,110,49,186,133,170,11,92,95,47,139,191,180,129,46,63,47,254,179,96,178,129,98,127,19,117,99,116,155,169,31,235,177,128,244,243,217,181,190,209,57,221,97,169,225,222,231,52,32,155,53,118,175,229,205,31,235,59,152,167,3,82,63,188,97,91,248,228,166,239,171,151,231,203,2,54,77,51,250,6,91,254,11,204,242,34,214,111,196,174,117,11,184,213,223,72,191,249,174,253,159,115,208,175,86,230,179,87,125,223,195,227,203,166,111,134,227,94,198,233,235,41,227,108,212,88,3,52,109,15,214,119,48,175,93,65,248,171,45,126,201,117,237,136,151,136,13,2,114,52,221,21,108,112,235,84,92,175,92,2,50,35,32,135,51,120,46,25,180,163,222,66,109,17,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,64,64,32,16,16,8,4,4,2,1,129,85,109,251,31,59,100,36,232,53,105,207,30,0,0,0,0,73,69,78,68,174,66,96,130
Run directly on IIS v8.5.9600.16384:
137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,200,0,0,0,100,8,6,0,0,0,195,134,127,11,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0,0,9,112,72,89,115,0,0,14,195,0,0,14,195,1,199,111,168,100,0,0,0,101,73,68,65,84,120,94,237,193,49,1,0,0,0,194,160,245,79,109,12,31,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,184,168,1,56,243,0,1,238,54,186,14,0,0,0,0,73,69,78,68,174,66,96,130
I tried different types of ImageFormat but with no success.
Anyone can say what is going on?
Very simple test:
var img = new Bitmap("<image-path>");
Response.Write(string.join(",", imageToByteArray(result)));
EDIT:
After some tests I found what is causing this problem.
private Bitmap _bitmap;
private void StartBrowser(string source)
{
var th = new Thread(() =>
{
var webBrowser = new WebBrowser();
webBrowser.ScrollBarsEnabled = false;
webBrowser.DocumentCompleted +=
webBrowser_DocumentCompleted;
webBrowser.DocumentText = source;
System.Windows.Forms.Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
public void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = (WebBrowser)sender;
_bitmap = new Bitmap(webBrowser.Width, webBrowser.Height))
webBrowser.DrawToBitmap(_bitmap, new System.Drawing.Rectangle(0, 0, _bitmap.Width, _bitmap.Height));
}
Only the Bitmaps created by this method has the mentioned problem.