views:

2143

answers:

4

Is there anyone who knows a way to resize images on the .net 2.0 Compact Framework?

I want to be able to get images, taken with the camera on my phone, from the phones memory, resize them and then upload them to a webservice so I acctually don't need to store the resized images on disk.

+1  A: 

A simple VB.NET solution, or a more complete treatment in C#. Google is your friend.

Pontus Gagge
So, what's the downvote about? Both links should work for the CF.
Pontus Gagge
The one for VB.net doesn't work for jpg-images apparently. The C#-code is for a desktop application.
Zooking
The C# code works fine with CF.NET 2.0 (unless there's a huge cover-up of the device emulator bluffing us all). Why don't you cut-n-paste and try it?
Pontus Gagge
+1  A: 

Graphics.FromImage should work for jpg images, however you may run into memory problems.

Have a look at this forum post for some ideas.

kgiannakakis
Quoting a post from the forum:"I was hoping for a .net example that calls the DLL. I've never done anything in the CE environment with c or c++ and am hoping not to have to"That statement fits for me to, thanks anyway.
Zooking
A: 

THis vb.net works on Windows Mobile. Only constraint: Large Bitmaps cause an OutOfMemoryException when opened:

   Private Function ResizedImage(ByVal image As Bitmap, ByVal maxW As Integer, ByVal maxH As Integer) As Bitmap
    Dim divideByH, divideByW As Double
    Dim width, height As Integer
    divideByW = image.Width / maxW
    divideByH = image.Height / maxH
    If divideByW > 1 Or divideByH > 1 Then
        If divideByW > divideByH Then
            width = CInt(CDbl(image.Width) / divideByW)
            height = CInt(CDbl(image.Height) / divideByW)
        Else
            width = CInt(CDbl(image.Width) / divideByH)
            height = CInt(CDbl(image.Height) / divideByH)
        End If
        Dim scaled As New Bitmap(width, height)
        Dim g As Graphics
        g = Graphics.FromImage(scaled)
        g.DrawImage(image, New Rectangle(0, 0, width, height), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
        g.Dispose()
        ResizedImage = scaled
    Else
        ResizedImage = image
    End If

End Function
+1  A: 

This is the C# code I use to resize images in my .NET CF 2.0 applications:

public static Image ResizePicture( Image image, Size maxSize )
{
  if( image == null )
    throw new ArgumentNullException( "image", "Null passed to ResizePictureToMaximum" );

  if( ( image.Width > maxSize.Width ) || ( image.Height > maxSize.Height ) )
  {
    Image resizedImage = new Bitmap( maxSize.Width, maxSize.Height );

    using( Graphics graphics = Graphics.FromImage( resizedImage ) )
    {
      graphics.Clear( Color.White );

      float widthRatio = maxSize.Width / image.Width;
      float heightRatio = maxSize.Height / image.Height;

      int width = maxSize.Width;
      int height = maxSize.Height;

      if( widthRatio > heightRatio )
      {
        width = ( int )Math.Ceiling( maxSize.Width * heightRatio );
      }
      else if( heightRatio > widthRatio )
      {
        height = ( int )Math.Ceiling( maxSize.Height * widthRatio );
      }

      graphics.DrawImage(
        image,
        new Rectangle( 0, 0, width, height ),
        new Rectangle( 0, 0, image.Width, image.Height ),
        GraphicsUnit.Pixel );
    }

    return resizedImage;
  }

  return image;
}
Martin Plante
This looks interesting, unfortunately I wont be able to try it out until in a couple of weeks.BR, Larre
Zooking