如何有效裁剪和缩放图像数据 [英] How to effectively crop and scale image data
问题描述
我需要处理图像中的像素值.因此,我写了这样一堂课:
I need to operate on pixel values in images. Therefore I wrote such a class:
public class image_class
{
#region property
public int width { get; private set; }
public int height { get; private set; }
byte[] buffer;
#endregion
#region constructors
public image_class()
{
;
}
public image_class(int Width, int Height, byte[] data)
{
if (data.Count() == (Width * Height * 3))
{
this.width = Width;
this.height = Height;
this.buffer = data;
}
else
throw new Exception();
}
#endregion
#region get bitmap
public ImageSource bmp
{
get
{
try
{
var pixelFormat = PixelFormats.Rgb24;
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
var stride = bytesPerPixel * width;
var bitmap = BitmapSource.Create(width, height, 96d, 96d, pixelFormat, null, buffer, stride);
return bitmap;
}
catch { return null; }
}
}
#endregion
#region methods
public void load_from_file(string path)
{
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(ofd.FileName, UriKind.Absolute);
bmp.EndInit();
height = (int)bmp.Height;
width = (int)bmp.Width;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
int size = height * stride;
var buffer2 = new byte[size];
bmp.CopyPixels(buffer2, stride, 0);
buffer = new byte[size * 3 / 4];
for (int i = 0, j = 0; i < size; i += 4, j += 3)
{
buffer[j] = buffer2[i + 2];
buffer[j + 1] = buffer2[i + 1];
buffer[j + 2] = buffer2[i];
}
}
public void load_from_file(string path, int Width, int Height)
{
this.width = Width;
this.height = Height;
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.DecodePixelHeight = height;
bmp.DecodePixelWidth = width;
bmp.UriSource = new Uri(path, UriKind.Absolute);
bmp.EndInit();
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
int size = height * stride;
var buffer2 = new byte[size];
bmp.CopyPixels(buffer2, stride, 0);
buffer = new byte[size * 3 / 4];
for (int i = 0, j = 0; i < size; i += 4, j += 3)
{
buffer[j] = buffer2[i + 2];
buffer[j + 1] = buffer2[i + 1];
buffer[j + 2] = buffer2[i];
}
}
public void save_to_file(string path)
{
var pixelFormat = PixelFormats.Rgb24;
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
var stride = bytesPerPixel * width;
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(width, height, 96d, 96d, pixelFormat, null, buffer, stride)));
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
encoder.Save(fs);
fs.Close();
}
public image_class cut(int x0, int y0, int dx, int dy)
{
if (x0 >= 0 & y0 >= 0 & x0 + dx <= width & y0 + dy <= height)
{
byte[] buffer3 = new byte[dx * dy * 3];
for (int y = 0, i = 0, j = -1; y < height; y++) for (int x = 0; x < width; x++, i += 3)
if (x >= x0 & x < x0 + dx & y >= y0 & y < y0 + dy)
{
buffer3[++j] = buffer[i];
buffer3[++j] = buffer[i + 1];
buffer3[++j] = buffer[i + 2];
}
return new image_class(dx, dy, buffer3);
}
else
throw new Exception();
}
#endregion
}
缓冲区数组是我使用的数组.
The buffer array is the one I use.
该课程非常好. 当我需要处理图像片段中的数据时,可以使用cut方法,例如:
This class works perfectly fine. When I need to operate on data from fragment of an image, I use method cut, for example:
image_class img1=new image_class();
image_class img2=img1.cut(5,5,20,20);
我需要的是将剪切的图像缩放到其他值并保持像素数据的格式.例如,我想要一张图片的50x50正方形,缩放到20x20.这就是我要做的:
What I need is to scale cut image to other values and maintain format of pixel data. For example I want 50x50 square from a picture, scaled to 20x20. This is what I do:
image_class img1=new image_class();
image_class img2=img1.cut(5,5,50,50);
img2.save_to_file(path);
image_class img3=new image_class();
img3.load_from_file(path, 20,20);
它有效,但是当然效果很差.谁能告诉我更好的解决方案?
It works, but of course is very uneffective. Can anyone show me better solution?
推荐答案
您不需要所有这些.为了从源位图中复制矩形部分并随后对其进行缩放,您可以简单地使用CroppedBitmap
和TransformedBitmap
:
You don't need all this. In order to copy a rectangular part from a source bitmap, and subsequently scale it, you might simply use a CroppedBitmap
and a TransformedBitmap
:
BitmapSource original = ...
var crop = new CroppedBitmap(original, new Int32Rect(5, 5, 50, 50));
var result = new TransformedBitmap(crop, new ScaleTransform(0.4, 0.4));
这篇关于如何有效裁剪和缩放图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!