2

i have the following code extracted from my source which is relevant for this:

using (Bitmap spriteBitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb))
{
        using (Graphics spriteGraphics = Graphics.FromImage(spriteBitmap))
        {
                Rectangle imageRect = new Rectangle(0, 0, imageInfo.Width, imageInfo.Height);

                using (Bitmap clonedImageBitmap = imageInfo.ImageBitmap.Clone(imageRect, spriteBitmap.PixelFormat))
                {
                        clonedImageBitmap.SetResolution(spriteBitmap.HorizontalResolution, spriteBitmap.VerticalResolution);

                        spriteGraphics.DrawImage(
                                clonedImageBitmap,
                                mappedImageInfo.X, mappedImageInfo.Y,
                                imageRect,
                                GraphicsUnit.Pixel);

                                spriteGraphics.Flush(FlushIntention.Flush);
                }

                imageInfo.DisposeBitmap();
        }
}

This code works perfect locally with the compute emulator of azure. But when i deploy it and execute the code it doesn't work anymore and it fails at spriteGraphics.DrawImage with the famous ArgumentException "Parameter is not valid".

For testing purposes i logged the details of the clonedImageBitmap to see if something is different. The only thing i found which is not the same locally and in the cloud are the .Flags -> locally i have 77842 and in the cloud 77846 so it seems that ImageFlagsHasTranslucent is set, but if that causes the problem in the cloud and how this can be is beyond my knowledge at the moment.

Perhaps somebody can help me with the strange problem?

Thanks in advance.

HeManNew

HeManNew
  • 233
  • 2
  • 10

1 Answers1

1

I would say, it sure is hard issue is to work due to the nature of application and where it is running.

The main issue here is that the classes/API you are using are coming from System.Drawing namespace and you must know that the classes in this namespace were designed for use with Windows Forms. They are not supported for use within a Windows or ASP.NET service and that's why as long as you can refrain using these API in a Windows Azure Web Role or Worker Role that is the best option. Cloud application are mainly web application or application which are working in background that is main reason avoiding class/api dependency on System.Drawing is the key which is further depend on GDI to make the situation more complex.

If API is failing, that is simply the reason that it is not compatible in web environment even when it is available. Even in Windows Azure Documentation it is suggested to avoid using API/Class from System.Drawing altogether and if it is must to have in your code, you should conduct exhaustive testing if you intend to use these classes in your own Windows Azure applications.

AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65
  • Thanks for your explanation. Unfortunately i haven't known that using System.Drawing with Azure is not advisable, but that feature (dynamically generating Css Sprites) is necessary for my application, so i am not able to exclude that. But what is weird to me that everything is running fine in local development in the compute emulator. – HeManNew Sep 20 '12 at 08:30
  • The reason is very simple as System.Drawing class is depend on GDI which is based on display specific component i.e. lower layer libraries and driver. And this is the key difference between your machine and the Windows Azure Machine. GDI specific module in a cloud machine could not match the performance & capabilities at use desktop and that why there is caution for using those API – AvkashChauhan Sep 20 '12 at 23:12