开发者

Adapt asp.net code to resize image

开发者 https://www.devze.com 2023-01-04 19:51 出处:网络
After searching, I\'ve discovered this code: Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)

After searching, I've discovered this code:

Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
    Dim image__1 = System.Drawing.Image.FromStream(fr开发者_运维知识库omStream)
    Dim newWidth = CInt(image__1.Width * scaleFactor)
    Dim newHeight = CInt(image__1.Height * scaleFactor)
    Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)

    Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
    thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
    thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
    thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

    Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
    thumbnailGraph.DrawImage(image__1, imageRectangle)
    thumbnailBitmap.Save(toStream, image__1.RawFormat)

    thumbnailGraph.Dispose()
    thumbnailBitmap.Dispose()
    image__1.Dispose()
End Sub

There are 2 things I can't "modify" to solve my problem:

  1. I wouldn't like to pass a stream, but I prefer to pass a path like C:\mysite\photo\myphoto.gif. How can I "convert" it to accept a file and not a stream?
  2. In this function I've to pass a "scale" value. But I prefer to check if the image is too big (for example > 1024x768) than resize it to a max of 1024x768. How can I check this with System.Drawing.

As you can see I don't know anything about System.Drawing so I need an "hard" help to solve this job.


Here is some c# code I did about 5 years ago to do this (it should still work I hope as the app hasn't been touched since). I think it does everthing you need but it doesn't upscale the image to 1024x768 if it is smaller. This code will only make sure that if it is larger than 1024x768, it will resize proportionally to fit within those dimensions:

const int maxWidth = 1024;
const int maxHeight = 768;
Image newImage = Image.FromFile("YourPicture.jpg");
double percentToShrink = -1;
if (newImage.Width >= newImage.Height)
{
    // Do we need to resize based on width?
    if (newImage.Width > maxWidth)
    {
        percentToShrink = (double)maxWidth / (double)newImage.Width;
    }
}
else
{
    // Do we need to resize based on width?
    if (newImage.Height > maxHeight )
    {
        percentToShrink = (double)maxHeight  / (double)newImage.Height;
    }
}

int newWidth = newImage.Width;
int newHeight = newImage.Height;

// So do we need to resize?
if (percentToShrink != -1)
{
    newWidth = (int)(newImage.Width * percentToShrink);
    newHeight = (int)(newImage.Height * percentToShrink);
}

// convert the image to a png and get a byte[]
MemoryStream imgStream = new MemoryStream();
Bitmap bmp = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.InterpolationMode =   System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, newWidth, newHeight);
    g.DrawImage(newImage, 0, 0, newWidth, newHeight);
}

// This can be whatever format you need
bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] imgBinaryData = imgStream.ToArray();
imgStream.Dispose();

If you need to convert this to VB.NET, you can use the C# to VB.NET converter here.


First question:

Dim newImage As Image = Image.FromFile("SampImag.jpg")

Second question:

Build a private method that will return you a Size object based on the original Size object of the given image. You can add a "keep proportions" flag also if you wish.

0

精彩评论

暂无评论...
验证码 换一张
取 消