C# 图像处理,不显示变换后的图像
我需要在不使用C中的GDI库的情况下以缩放、旋转和平移的方式显示文件中的图像。我尝试使用TransformedBitmap对象和WPF进行转换 编辑:我有权使用GDI 以下是两种尝试: 转换位图:C# 图像处理,不显示变换后的图像,c#,wpf,C#,Wpf,我需要在不使用C中的GDI库的情况下以缩放、旋转和平移的方式显示文件中的图像。我尝试使用TransformedBitmap对象和WPF进行转换 编辑:我有权使用GDI 以下是两种尝试: 转换位图: BitmapSource mirroredImage = //... // Figure out the center of the image double centerX = ((double) mirroredImage.Width) / 2.0; double centerY = ((doub
BitmapSource mirroredImage = //...
// Figure out the center of the image
double centerX = ((double) mirroredImage.Width) / 2.0;
double centerY = ((double) mirroredImage.Height) / 2.0;
// Scale the image
TransformedBitmap scaledImage = new TransformedBitmap();
scaledImage.BeginInit();
scaledImage.Source = mirroredImage;
scaledImage.Transform = new ScaleTransform(test.scale, test.scale, centerX, centerY);
scaledImage.EndInit();
// Rotate the image (from the center of the image)
TransformedBitmap rotatedImage = new TransformedBitmap();
rotatedImage.BeginInit();
rotatedImage.Source = scaledImage;
rotatedImage.Transform = new RotateTransform(test.angle, centerX, centerY);
rotatedImage.EndInit();
// Translate the image
TransformedBitmap translatedImage = new TransformedBitmap(;
translatedImage.BeginInit();
translatedImage.Source = rotatedImage;
translatedImage.Transform = new TranslateTransform(test.xShift, test.yShift);
translatedImage.EndInit();
// Display the transformed image
int width = (int)translatedImage.Source.Width;
int height = (int)translatedImage.Source.Height;
int stride = width * ((translatedImage.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
translatedImage.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
width,
height,
mstride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
ptr);
picMain.Image = bitmap; // picMain is a PictureBox in the Form
}
}
this.Refresh() // This is in a class inheriting Form
WPF:
所有这些的结果只是显示原始镜像图像。如何显示转换后的图像,translatedImage?函数在我的表单类中来自: 执行转换:
Bitmap inputImage = (Bitmap) Bitmap.FromFile(inputPath);
Bitmap mirroredImageGDI = new Bitmap(3 * inputImage.Width, 3 * inputImage.Height);
Graphics mirroredImGfx = Graphics.FromImage(mirroredImageGDI);
mirroredImGfx.DrawImage(inputImage, //...
Bitmap scaledImage = scaleImage(mirroredImageGDI, test);
Bitmap rotatedImage = rotateImage(scaledImage, test);
Bitmap translatedImage = translateImage(rotatedImage, test);
picMain.Image = translatedImage; // PictureBox in my Form
只是好奇。。。为什么你不能使用GDI?一些与转换过程中不可预测的像素映射有关的东西,不完全确定,只是我被分配完成的任务。请不要在你的标题前面加上C之类的前缀。这就是标签的作用。@vlad417:我不知道在变换过程中与不可预测的像素映射相关的东西意味着什么。在提出过于复杂的解决方案来避免GDI之前,您可能应该理解为什么GDI不是一个选项。我已经讨论过了,这是一个个人偏好,一开始我没有明确说明。我现在可以使用GDI并尝试使用GDI编辑我的问题。
private Bitmap rotateImage(Bitmap b, TestSettings test)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//rotate
g.RotateTransform(test.angle);
//move image back
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new System.Drawing.Point(0, 0));
return returnBitmap;
}
private Bitmap scaleImage(Bitmap b, TestSettings test)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//rotate
g.ScaleTransform( test.scale, test.scale );
//move image back
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new System.Drawing.Point(0, 0));
return returnBitmap;
}
private Bitmap translateImage(Bitmap b, TestSettings test)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move image
g.TranslateTransform( test.xShift, test.yShift );
//draw passed in image onto graphics object
g.DrawImage(b, new System.Drawing.Point(0, 0));
return returnBitmap;
}
Bitmap inputImage = (Bitmap) Bitmap.FromFile(inputPath);
Bitmap mirroredImageGDI = new Bitmap(3 * inputImage.Width, 3 * inputImage.Height);
Graphics mirroredImGfx = Graphics.FromImage(mirroredImageGDI);
mirroredImGfx.DrawImage(inputImage, //...
Bitmap scaledImage = scaleImage(mirroredImageGDI, test);
Bitmap rotatedImage = rotateImage(scaledImage, test);
Bitmap translatedImage = translateImage(rotatedImage, test);
picMain.Image = translatedImage; // PictureBox in my Form