图像处理,不会显示变换后的图像

时间:2012-03-20 18:33:50

标签: c# wpf

我需要在不使用C#中的GDI库的情况下,按照缩放,旋转和翻译的方式显示文件中的图像。我尝试使用TransformedBitmap个对象和WPF进行转换。

编辑:我继续使用GDI ......

以下是2次尝试:

TransformedBitmap:

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:

int mwidth = mirroredImage.PixelWidth;
int mheight = mirroredImage.PixelHeight;
int mstride = mwidth * ((mirroredImage.Format.BitsPerPixel + 7) / 8);

byte[] mbits = new byte[mheight * mstride];

mirroredImage.CopyPixels(mbits, mstride, 0);

ImageSource scaledImageSrc = BitmapSource.Create(mirroredImage.PixelWidth, mirroredImage.PixelHeight, mirroredImage.DpiX, mirroredImage.DpiY, mirroredImage.Format, mirroredImage.Palette, mbits, mstride);

System.Windows.Controls.Image scaledImage = new System.Windows.Controls.Image();
scaledImage.Source                        = scaledImageSrc;

scaledImage.RenderTransform = new ScaleTransform(test.scale, test.scale, centerX, centerY);

System.Windows.Controls.Image rotatedImage = new System.Windows.Controls.Image();
rotatedImage.Source                        = scaledImage.Source;

rotatedImage.RenderTransform = new RotateTransform(test.angle, centerX, centerY);

System.Windows.Controls.Image translatedImage = new System.Windows.Controls.Image();
translatedImage.Source                        = rotatedImage.Source;

translatedImage.RenderTransform = new TranslateTransform(test.xShift, test.yShift);

Window main = new Window();
main.Title = "Test Image";

// Create a Canvas to contain the Image.
Canvas canvas1 = new Canvas();
canvas1.Width = 200;
canvas1.Height = 200;
Canvas.SetLeft(translatedImage, 0);
Canvas.SetTop(translatedImage, 0);
canvas1.Children.Add(translatedImage);

main.Content = canvas1;
main.ShowDialog();

所有这些的结果只是显示原始mirroredImage。如何显示转换后的图像translatedImage

1 个答案:

答案 0 :(得分:0)

我的Form类中的函数(来自tutorial):

    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