7

(PictureBoxesで、Graphics.DrawImage()を使用して)C#フォームに画像を描画しようとしていますが、スムーズに描画する方法を探しています。画像は透明度をサポートする形式である必要があるため、PNG、GIF、SVG、およびWMFです。C#はSVGファイルをすぐにサポートしておらず、使用するのに適したサードパーティのライブラリが見つかりませんでした(SvgNetを見つけましたが、理解できませんでした)。

C#がImage.FromFile()関数を介して実行できるWMFファイルを描画する必要がありますが、アンチエイリアス処理されていません。これをスムーズにする方法があるかどうか疑問に思いましたか?

4

2 に答える 2

16

以前の回答は、よく意図されていましたが、部分的にしか正しくありませんでした。

何が正しかったですか?PictureBoxはInterpolationModeを公開しません。

何がオフベースでしたか?

1)そのプロパティは、画像ボックスから、その親で、または派生クラスのオーバーライドを介して、Paintイベントで簡単に設定できます。。。どちらの方法でも機能し、どちらも同じように簡単です。ただし、SmoothingModeが設定されていない限り、InterpolationModeは無視されます。SmoothingModeをSmoothingMode.AnitAliasに設定​​しないと、アンチエイリアシングは取得されません。

2)PictureBoxの機能を使用することに明確に関心を示したときにパネルを使用することは、間違った方向です。これらのプロパティを明示的にコーディングせずに、画像を直接読み込んだり、保存したり、割り当てたりすることはできません。。。なぜ車輪の再発明をするのですか? PictureBoxから派生することにより、それらすべてを無料で入手できます。

私があなたのために一生懸命働いて、このメッセージを書くよりも時間がかからなかったので、 ニュースはさらに良くなりました。

どちらもPictureBoxから派生した2つのバージョンを提供しました。1つ目は、常に可能な限り最高品質のレンダリングを使用する簡単な例です。これは最も遅いレンダリングでもあります。2つ目は、派生クラスのプロパティを介して誰でもさまざまなレンダリングパラメータを設定できるようにするクラスです。一度設定すると、これらはOnPaintオーバーライドで使用されます。

public class HighQualitySmoothPictureBox : PictureBox
{
    protected override void OnPaint(PaintEventArgs pe)
    {
        // This is the only line needed for anti-aliasing to be turned on.
        pe.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        // the next two lines of code (not comments) are needed to get the highest 
        // possible quiality of anti-aliasing. Remove them if you want the image to render faster.
        pe.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        pe.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        // this line is needed for .net to draw the contents.
        base.OnPaint(pe);
    }
}

..。

public class ConfigurableQualityPictureBox : PictureBox
{
    // Note: the use of the "?" indicates the value type is "nullable."  
    // If the property is unset, it doesn't have a value, and therefore isn't 
    // used when the OnPaint method executes.
    System.Drawing.Drawing2D.SmoothingMode? smoothingMode;
    System.Drawing.Drawing2D.CompositingQuality? compositingQuality;
    System.Drawing.Drawing2D.InterpolationMode? interpolationMode;

    public System.Drawing.Drawing2D.SmoothingMode? SmoothingMode
    {
        get { return smoothingMode; }
        set { smoothingMode = value; }
    }

    public System.Drawing.Drawing2D.CompositingQuality? CompositingQuality
    {
        get { return compositingQuality; }
        set { compositingQuality = value; }
    }

    public System.Drawing.Drawing2D.InterpolationMode? InterpolationMode
    {
        get { return interpolationMode; }
        set { interpolationMode = value; }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        if (smoothingMode.HasValue)
            pe.Graphics.SmoothingMode = smoothingMode.Value;
        if (compositingQuality.HasValue)
            pe.Graphics.CompositingQuality = compositingQuality.Value;
        if (interpolationMode.HasValue)
            pe.Graphics.InterpolationMode = interpolationMode.Value;

        // this line is needed for .net to draw the contents.
        base.OnPaint(pe);
    }
}
于 2009-11-21T05:18:14.380 に答える
7

イメージをキャンバスに描画するとき、補間モードを最も近いものよりも適切なものに変更して、サイズ変更されたイメージをスムーズにすることができます。

Graphics g = ...
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(...);

InterpolationMode 列挙型を取得するには、System.Drawing.Drawing2D を追加する必要があります。

PictureBox を使用すると問題が発生します。これは InterpolationMode プロパティを公開しないため、独自に作成するかダウンロードする必要があります。

于 2009-02-19T17:21:22.610 に答える