围绕中心径向旋转若干图像

时间:2015-11-28 20:17:25

标签: c# wpf rotation image-rotation

我正在尝试构建一个应用程序,它可以加载n个图像并围绕中心径向转换它们,并在图像控制中显示结果。我可以通过定义每个位置来加载两个图像,并使用以下代码对其进行转换:

private void button1_Click(object sender, RoutedEventArgs e)
    {
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(@"Location\Car\Car89.png", UriKind.Absolute);
        src.CacheOption = BitmapCacheOption.OnLoad;
        src.EndInit();

        double deg = 90;
        double rad = deg / 180.0 * Math.PI;

        //get the bounds of the rotated image
        double w = Math.Abs(Math.Cos(-rad) * src.PixelWidth) + Math.Abs(Math.Sin(-rad) * src.PixelHeight);
        double h = Math.Abs(Math.Sin(-rad) * src.PixelWidth) + Math.Abs(Math.Cos(-rad) * src.PixelHeight);

        BitmapImage src2 = new BitmapImage();
        src2.BeginInit();
        src2.UriSource = new Uri(@"Location\Car\Car269.png", UriKind.Absolute);
        src2.CacheOption = BitmapCacheOption.OnLoad;
        src2.EndInit();

        double w2 = Math.Abs(Math.Cos(rad) * src2.PixelWidth) + Math.Abs(Math.Sin(rad) * src2.PixelHeight);
        double h2 = Math.Abs(Math.Sin(rad) * src2.PixelWidth) + Math.Abs(Math.Cos(rad) * src2.PixelHeight);

        Double radius = 50;

        //Size sz = new Size(w + w2 + radius * 2, h + h2 + radius * 2);

        double c1X = Math.Abs(Math.Sin(rad)) * (radius + src.PixelHeight / 2.0);
        double c1Y = Math.Abs(Math.Cos(rad)) * (radius + src.PixelHeight / 2.0);

        Size sz = new Size((w / 2 + c1X) * 2.0, (h / 2 + c1Y) * 2.0);

        DrawingVisual dv = new DrawingVisual();
        RenderOptions.SetBitmapScalingMode(dv, BitmapScalingMode.Fant);
        RenderTargetBitmap rtb = null;

        using (DrawingContext dc = dv.RenderOpen())
        {
            dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, (int)sz.Width, (int)sz.Height));
            dc.DrawEllipse(Brushes.White, null, new Point(sz.Width / 2.0, sz.Height / 2.0), radius, radius);
            //translate to first center
            dc.PushTransform(new TranslateTransform(sz.Width / 2.0 - c1X, sz.Height / 2.0 - c1Y));
            //rotate
            dc.PushTransform(new RotateTransform(-deg));
            //draw
            dc.DrawImage(src, new Rect(-src.PixelWidth / 2.0, -src.PixelHeight / 2.0, src.PixelWidth, src.PixelHeight));
            dc.Pop();
            dc.Pop();

            dc.PushTransform(new TranslateTransform(sz.Width / 2.0 + c1X, sz.Height / 2.0 - c1Y));
            dc.PushTransform(new RotateTransform(deg));
            dc.DrawImage(src2, new Rect(-src2.PixelWidth / 2.0, -src2.PixelHeight / 2.0, src2.PixelWidth, src2.PixelHeight));
            dc.Pop();
            dc.Pop();


        }

        rtb = new RenderTargetBitmap((int)sz.Width, (int)sz.Height, 96, 96, PixelFormats.Pbgra32);
        RenderOptions.SetBitmapScalingMode(rtb, BitmapScalingMode.Fant);

        rtb.Render(dv);

        this.image1.Source = rtb;
    }

但是我无法加载自动命名的多个图像,例如Car0.png,Car1.png,...,CarN.png

我怎样才能做到这一点? 此致 塞缪尔法里德

0 个答案:

没有答案