views:

584

answers:

2

Hi,

can any one tell me, how to load the highresolution images in windows mobile using c#. i am getting outofmemoryexception for that.

A: 

Use IImagingFactory and IImage, they are com objects that were introduced in WCE 5.0.

You will have to write com wrappers for them, or use OpenNETCF which has them already.

See here for a blog post from Chris Lorton on alphablending and the imaging factory classes and how to use them from .net

Matt
Thanks for your reply,i am also using this method wat u ment..but the problem is..IntPtr hdcDest=gxBuffer.GetHdc();Rectangle dstRect=new Rectangle(left,top,right,bottom);imagingImage.Draw(hdcDest,ref dstRect,IntPtr.Zero);gxBuffer.ReleaseHdc(hdcDest);e.Graphics.DrawImage(backBuffer,0,0);we get memoryoutofexception at"imagingImage.Draw(hdcDest, ref dstRect, IntPtr.Zero);"we don't crate bitmap like,using (Bitmap backBuffer = new Bitmap(Width, Height, PixelFormat.Format32bppRgb))insted,Bitmap backBuffer = new Bitmap(width,height)does PixelFormat.Format32bppRgb matters or not?
Shadow
I don't think so, check the sizes in your dstRect, because IImage expects right and bottom to be absolute positions, not width and height as in the constructor of rect. I have used the code you have there many times in my WM app, most times I got exceptions on drawing is when the dstRect is not set up right. How big is the IImage you are trying to draw and what format is it?
Matt
Thank u..image resolution is 3000X2000.i am using like thisbitmap = new Bitmap(width, height);graphics = Graphics.FromImage(bitmap);IntPtr hdcDest = graphics .GetHdc();Rectangle dstRect = new Rectangle(0, 0, bitmap .width, bitmap .height);Image.Draw(hdcDest, ref dstRect, IntPtr.Zero);
Shadow
What are your width and height values that you create the bitmap at? 3000x2000?
Matt
Ahh I think I had this problem too, using a large image and drawing it resized onto a small bitmap I still got OOM, however if you use IImage.GetThumbnail with the size of the smaller bitmap you want to draw the large image on, it will resize it for you.
Matt
i am getting the thumbnail image, but i want to draw the image on picturebox with 320X240 resolution..i am passing width and height as 320X240..there is no problem in getting thumbnail how much ever the resolution is.
Shadow
A: 

This code shows a form with a 2mb jpg image (3000x2000 or so) on a form at 240x320, without any OOM exceptions. Obviously you will need to tidy it up to dispose of the bmp and release the com objects etc. but it does work and I have tried it on the wm6.5 emulator.

public partial class Form1 : Form
{
    IImage smallImage;
    Bitmap bmp = new Bitmap(240, 320);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IImage fullSizeImage;
        IImagingFactory factory = new ImagingFactoryClass();
        factory.CreateImageFromFile(@"\My Documents\My Pictures\luminous opera house.jpg", out fullSizeImage);

        fullSizeImage.GetThumbnail(240, 320, out smallImage);

        using (Graphics gfx = Graphics.FromImage(bmp))
        {
            IntPtr hdc = gfx.GetHdc();
            try
            {
                smallImage.Draw(hdc, new Rectangle(0, 0, 240, 320), IntPtr.Zero);
            }
            finally
            {
                gfx.ReleaseHdc(hdc);
            }
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(bmp, 0, 0);
    }
}
Matt
I am using the opennetcf imaging library, hence the ImageFactoryClass, but i have changed the call to IImage.Draw to use a rectangle and intptr rather than RECT and null, as I think you will probably have to do if you aren't using opennetcf
Matt
we are not using "opennetcf", fullSizeImage.GetThumbnail(240, 320, out smallImage);as i have seen for bigger image pixelates, image quality will go down. draw API gives OOM
Shadow
I know you aren't using opennetcf, which is why i changed the call to Draw. They are just wrappers around the com anyway. And yes, the call to thumbnail will pixellate the image as it shrinks it. Do you need to do the image size adjustment on the device? Can't you size the image appropriately and include it with your app?
Matt
I think you should take a look at this question and the answer from Chris Tacke. Maybe you can use DIBs to load the large image and do the processing to shrink it. Also look at the IImageDecoders, maybe you need to use one of those that will resize better than GetThumbnail will...http://stackoverflow.com/questions/518119/windows-mobile-creating-a-device-independent-bitmap-by-size-in-cf-net
Matt
Thanks for reply,we saw but,it doesnt look good if we do like that,any other alternatives or any other way to load image..
Shadow
Yeah I noticed that the images can look a little fuzzy, the only other ways I can think are, as I mentioned, see if there is an existing decoder that can resize images nicely (there is an IBasicBitmapOps, not sure if that is the one that is used by GetThumbnail). Or use a device independant bitmap (which can be allocated outside of the process's 32mb virtual address space) and write the code yourself to process it. These are the ways I would look at, but as GetThumbnail was good enough for me it's stuff I haven't tried.
Matt
Thank u.. :-)yest i tried lot of methods to perform the same.. but nothing worked for me :-(.. do u have any other solution..
Shadow
Write your own image resizer?You could try p/invoking stretchblt (http://msdn.microsoft.com/en-us/library/dd145120%28VS.85%29.aspx) given that you can get the hdc of the IImage and your bitmap?I would suspect that all these will produce similarly fuzzy results and you might want to look at alternate resize algorithms...
Matt