0

必要なのは、同じ場所で解像度が異なる画像をトリミングすることです。

例えば:

1024×768で作成した画像1

1440×900で作成した画像2

今、私は画像をトリミングする必要がありますが、同じ場所でそれがされるとしましょう

X = 10% Y = 10% 幅 = 30% 高さ = 20%

次のコードを使用してそれを行いますが、必要なように機能しません。

どんな手掛かり?

ありがとうございました!!!

int x = 0;
int y = 0;
int w = 0;
int h = 0;

int inputX = 10;
int inputY = 10;
int inputW = 20;
int inputH = 30;

x = int.Parse(Math.Round(decimal.Parse((__Bitmap.Width * inputX / 100).ToString()), 0).ToString());
y = int.Parse(Math.Round(decimal.Parse((__Bitmap.Height * inputY / 100).ToString()), 0).ToString());

w = int.Parse(Math.Round(decimal.Parse((__Bitmap.Width * inputW / 100).ToString()), 0).ToString());
h = int.Parse(Math.Round(decimal.Parse((__Bitmap.Height * inputH / 100).ToString()), 0).ToString());

Rectangle cropArea = new Rectangle(x, y, w,h);
Bitmap bmpCrop = __Bitmap.Clone(cropArea, __Bitmap.PixelFormat);

技術的にそれを行うためのロジックがあるかどうかを意味しますか?

私は(疑似コード)のようにできると思います

if (Resolution == "1024x768")
    int inputX = 10;
    int inputY = 10;
    int inputW = 20;
    int inputH = 30;
else if (Resolution == "1440x900")
    int inputX = 8;
    int inputY = 8;
    int inputW = 19;
    int inputH = 28;

 and etc...

解像度に応じて % を再計算する係数があるかどうかはわかりません...それはクロップファクターのようなものです。

アップデート:

ここに画像の説明を入力

4

2 に答える 2

2

私が意味することの簡単で汚い例は、パーセンテージでクロップウィンドウを計算するとき、常に同じ画像セクションを取得します:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // 100x80 image
        Image asdf = Image.FromFile("asdf.bmp", true);

        // twice the size, 200x160
        Image asdf2 = Image.FromFile("asdf2.bmp", true);

        // same image, different aspect ratio: 200x80
        Image asdf3 = Image.FromFile("asdf3.bmp", true);

        Bitmap asdfBmp = new Bitmap(asdf);
        Bitmap asdf2Bmp = new Bitmap(asdf2);
        Bitmap asdf3Bmp = new Bitmap(asdf3);

        pictureBox1.Image = cropImage(asdfBmp);
        pictureBox2.Image = cropImage(asdf2Bmp);
        pictureBox3.Image = cropImage(asdf3Bmp);
    }

    private Bitmap cropImage(Bitmap sourceBitmap)
    {
        double x = 0;
        double y = 0;
        double w = 0;
        double h = 0;

        double inputX = 10;
        double inputY = 10;
        double inputW = 50;
        double inputH = 50;

        // integer division " 10 / 100 " will return 0, use doubles or floats.
        // furthermore you don't have to convert anything to a string or back here.
        x = sourceBitmap.Width * inputX / 100.0;
        y = sourceBitmap.Height * inputY / 100.0;

        w = sourceBitmap.Width * inputW / 100.0;
        h = sourceBitmap.Height * inputH / 100.0;

        // casting to int will just cut off all decimal places. you could also round.
        Rectangle cropArea = new Rectangle((int)x, (int)y, (int)w, (int)h);
        return sourceBitmap.Clone(cropArea, sourceBitmap.PixelFormat);
    }
}

ソース:

asdf.bmp asdf2.bmp asdf3.bmp

結果:

結果

ご覧のとおり、すべての結果画像は画像の同じセクションを示しています。だから私はあなたが目指しているものをまだ得ていないか、あなたのエラーは別の場所にあるに違いありません.

不要な型変換と整数除算のバグを考慮すると、おそらくtypes に関する c# のチュートリアルを参照する必要があります。

于 2012-11-20T18:40:17.387 に答える
1

最初にクロップの中心を計算します。必要な x、y、w、h 値を何らかの方法で取得すると仮定します。次に、この中心点を 2 番目の画像の中心に再計算する必要があります。つまり、中心が [25;50] の場合、1024x768 画像の場合はそれぞれ [25/1024;50/768] となり、[2.44%] になります。 ;6.51%]。したがって、2 番目の画像では、1440x900 としましょう。もちろん、[1440*2.44%;900*6.51%] => [35;59] がピクセル単位で表示されます。

次に、新しい画像の幅と高さが必要です。縦横比が同じ場合は、寸法を として計算できるので簡単ですがcropWidth/firstImageWidth*secondImageWidth、それ以外の場合は、正しい縦横比を掛ける必要があります。

とにかく、あなたは問題を理解していないと思います。類似した画像の縦横比が異なる場合は、画像の一部が異なるか、画像が歪んでいます。

以下に、あなたの例を修正しました。当たり前のことなので説明はしませんが.. 願っています。透明な黒と白の領域で覆われた部分を見てください...

ここに画像の説明を入力

于 2012-11-21T23:42:02.183 に答える