1

検索した後、次のコードを発見しました。

Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
    Dim image__1 = System.Drawing.Image.FromStream(fromStream)
    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

問題を解決するために「変更」できないことが 2 つあります。

  1. ストリームを渡すのは好きではありませんが、のようなパスを渡すことを好みますC:\mysite\photo\myphoto.gif。ストリームではなくファイルを受け入れるように「変換」するにはどうすればよいですか?
  2. この関数では、「スケール」値を渡す必要があります。しかし、私は画像が大きすぎるかどうかを確認することを好みます (たとえば > 1024x768) 1024x768。でこれを確認するにはどうすればよいですかSystem.Drawing

ご覧のとおり、私は System.Drawing について何も知らないので、この仕事を解決するには「難しい」助けが必要です。

4

2 に答える 2

0

これを行うために約5年前に行ったコードを次に示しc#ます(アプリはそれ以来触れられていないので、まだ機能するはずです)。必要なことはすべて行うと思いますが、画像が小さい場合は画像を 1024x768 に拡大しません。このコードは、サイズが 1024x768 より大きい場合に、それらのサイズに収まるように比例してサイズ変更されることのみを確認します。

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();

これを VB.NET に変換する必要がある場合は、ここで C# から VB.NET へのコンバーターを使用できます

于 2010-06-28T15:46:21.503 に答える
0

最初の質問:

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

2 番目の質問:

指定された画像の元の Size オブジェクトに基づいて Size オブジェクトを返すプライベート メソッドを構築します。必要に応じて、「プロポーションを維持」フラグを追加することもできます。

于 2010-06-28T15:25:46.053 に答える