how to compress image to width and height?

mc 5,161 Reputation points
2024-11-21T00:48:22.3233333+00:00

I have many images. and I want to compress it.

If the image is (300,500)

I want to compress it to (30,50) to (90,150) to (12,60)

how to compress it?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,935 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 35,826 Reputation points Microsoft Vendor
    2024-11-21T03:15:05.7233333+00:00

    Hello,

    On Android, if you need to resize an image, you need to convert it into a bitmap.

    Please refer to the following resizer tool code. You can use it directly to resize the image.

    
    public static byte[] ResizeImageOnAndroid (byte[] imageData, float width, float height)
    
    {
    
    	// Step 1. Load the bitmap
    
    	Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
    
          // Sets the resized size.
    
    	Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);
    
    	using (MemoryStream ms = new MemoryStream())
    
    	{
    
                // Compress the original image to jpeg format with 100 quality.
    
    		resizedImage.Compress (Bitmap.CompressFormat.Jpeg, 100, ms);
    
    		return ms.ToArray ();
    
    	}
    
    }
    
    

    Best Regards, Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.