1

I have one image runat server,with id => myImageId, Image src is in base64

var convertedImage = Convert.ToBase64String(
                    File.ReadAllBytes(somePath),
                );
myImageId.Src = @"data:image/gif;base64," + convertedImage ;

I want to resize this image if is very big (max_width = 400; max_height = 400), in aspx page I can to set attribute 'max-width' and 'max_height' but converted image have same resolution, how I can to resize image before convert to base 64 to set max width and height?

Alex
  • 8,908
  • 28
  • 103
  • 157

1 Answers1

2

how I can to resize image before convert to base 64

By not passing the file directly to Convert.ToBase64String(), but loading it as an image and resizing it first.

See Loading a picture file Image.FromFile VS FileStream to create an Image instance from a file, see Resize an Image C# to get a resized Bitmap instance, and see Convert a bitmap into a byte array to convert that bitmap to a byte array that you can feed to Convert.ToBase64String.

And then you'll want to look into caching, because you don't want to resize images on each request. Preferably your resizing and caching code work together to minimize the amount of code needed and optimize the interoperability. Libraries exist to do that for you. Read more at Resize and Display image from server with ASP.NET.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272