What I need is to crop images at the same place but with different resolution.
我需要的是在同一个地方裁剪图像,但分辨率不同。
For example:
例如:
Image 1 created with 1024 x 768
图像1使用1024 x 768创建
Image 2 created with 1440 x 900
图像2使用1440 x 900创建
Now I have to crop images but at the same place let's say it will be
现在我必须裁剪图像,但在同一个地方让我们说它会
X = 10% Y = 10% WIDTH = 30% HEIGHT = 20%
X = 10%Y = 10%WIDTH = 30%HEIGHT = 20%
I use the following code to do it but it doesn't work like I need.
我使用以下代码来执行此操作,但它不能像我需要的那样工作。
Any clue?
任何线索?
THANK YOU!!!
谢谢!!!
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);
I mean if there technically logic to do it?
我的意思是,如果有技术逻辑吗?
I guess I can do like (pseudo-code)
我想我可以做(伪代码)
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...
I am not sure of there is any coefficient to recalculate % depending on resolution to do it... It is like a crop-factor.
我不确定是否有任何系数可以根据分辨率重新计算%...它就像一个裁剪因素。
UPDATE:
更新:
2 个解决方案
#1
2
A quick and dirty example of what I mean by, you'll always get the same image section, when calculating your crop window with percentages:
在计算具有百分比的裁剪窗口时,我的意思是一个快速而肮脏的示例,您将始终获得相同的图像部分:
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);
}
}
Sources:
资料来源:
Result:
结果:
As you can see, all result images show the same section of the image. So I either still don't get what you're aiming at, or your error must be somewhere else.
如您所见,所有结果图像都显示图像的相同部分。所以我要么仍然没有得到你的目标,要么你的错误必须在其他地方。
Considering your unnecessary type conversions and integer division bug, you should perhaps have a look at a c# tutorial about types.
考虑到您不必要的类型转换和整数除法错误,您或许应该查看有关类型的c#教程。
#2
1
First calculate the center of the crop. I assume that you get somehow the required x,y,w,h values. Then this center point need to be recalculated to the center of the second image: i.e. if the center is [25;50], then for the 1024x768 image it is respectively [25/1024;50/768], which gives [2.44%;6.51%]. So on the second image, let's say 1440x900 it gives us [1440*2.44%;900*6.51%] => [35;59] in pixels of course.
首先计算作物的中心。我假设你得到了所需的x,y,w,h值。然后需要将该中心点重新计算到第二个图像的中心:即如果中心是[25; 50],则对于1024x768图像,它分别为[25/1024; 50/768],得到[2.44%] ; 6.51%]。所以在第二张图片上,让我们说1440x900它当然给我们[1440 * 2.44%; 900 * 6.51%] => [35; 59]像素。
Now you need width and height of the new image. If the aspect ratio is the same it is easy, because you can calculate dimensions as a cropWidth/firstImageWidth*secondImageWidth
, but otherwise you need to multiply it by the correct aspect ratio.
现在您需要新图像的宽度和高度。如果宽高比相同则很容易,因为您可以将尺寸计算为cropWidth / firstImageWidth * secondImageWidth,否则您需要将其乘以正确的宽高比。
Anyway I don't think you understand the problem. If the aspect ratios of similar images are different, it is either different part of the image or image is distorted.
无论如何,我不认为你理解这个问题。如果相似图像的纵横比不同,则图像的不同部分或图像失真。
Below I've corrected your example. I won't explain it because it's quite obvious.. I hope. Just take a look at the parts covered by the transparent black and white areas...
下面我已经纠正了你的例子。我不会解释它,因为它很明显..我希望。只需看看透明的黑色和白色区域所覆盖的部分......
#1
2
A quick and dirty example of what I mean by, you'll always get the same image section, when calculating your crop window with percentages:
在计算具有百分比的裁剪窗口时,我的意思是一个快速而肮脏的示例,您将始终获得相同的图像部分:
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);
}
}
Sources:
资料来源:
Result:
结果:
As you can see, all result images show the same section of the image. So I either still don't get what you're aiming at, or your error must be somewhere else.
如您所见,所有结果图像都显示图像的相同部分。所以我要么仍然没有得到你的目标,要么你的错误必须在其他地方。
Considering your unnecessary type conversions and integer division bug, you should perhaps have a look at a c# tutorial about types.
考虑到您不必要的类型转换和整数除法错误,您或许应该查看有关类型的c#教程。
#2
1
First calculate the center of the crop. I assume that you get somehow the required x,y,w,h values. Then this center point need to be recalculated to the center of the second image: i.e. if the center is [25;50], then for the 1024x768 image it is respectively [25/1024;50/768], which gives [2.44%;6.51%]. So on the second image, let's say 1440x900 it gives us [1440*2.44%;900*6.51%] => [35;59] in pixels of course.
首先计算作物的中心。我假设你得到了所需的x,y,w,h值。然后需要将该中心点重新计算到第二个图像的中心:即如果中心是[25; 50],则对于1024x768图像,它分别为[25/1024; 50/768],得到[2.44%] ; 6.51%]。所以在第二张图片上,让我们说1440x900它当然给我们[1440 * 2.44%; 900 * 6.51%] => [35; 59]像素。
Now you need width and height of the new image. If the aspect ratio is the same it is easy, because you can calculate dimensions as a cropWidth/firstImageWidth*secondImageWidth
, but otherwise you need to multiply it by the correct aspect ratio.
现在您需要新图像的宽度和高度。如果宽高比相同则很容易,因为您可以将尺寸计算为cropWidth / firstImageWidth * secondImageWidth,否则您需要将其乘以正确的宽高比。
Anyway I don't think you understand the problem. If the aspect ratios of similar images are different, it is either different part of the image or image is distorted.
无论如何,我不认为你理解这个问题。如果相似图像的纵横比不同,则图像的不同部分或图像失真。
Below I've corrected your example. I won't explain it because it's quite obvious.. I hope. Just take a look at the parts covered by the transparent black and white areas...
下面我已经纠正了你的例子。我不会解释它,因为它很明显..我希望。只需看看透明的黑色和白色区域所覆盖的部分......