English 中文(简体)
WPF: 维护和维护形象
原标题:WPF: Cut and save image

我寻求一种方式来轮换图像,以切断这一形象中的 rec,并把它作为一个单独的形象加以拯救。

“Illustration”/

www.un.org/Depts/DGACM/index_spanish.htm Edit: See received response for Solutions.

最佳回答

Update
I read your question again and this is probably closer to your problem. I didn t quite get how to use your coordinates but this method will take a BitmapImage, rotate it by an angle and save a cropped version of it from the starting X,Y values with the specified width and height. Probably not 100% like what you re trying to do but hopefully you can use the idea.

public void RotateAndSaveImage(BitmapImage sourceImage,
                               double angle,
                               int startX,
                               int startY,
                               int width,
                               int height,
                               string filePath)
{
    TransformGroup transformGroup = new TransformGroup();
    RotateTransform rotateTransform = new RotateTransform(angle);
    rotateTransform.CenterX = sourceImage.PixelWidth / 2.0;
    rotateTransform.CenterY = sourceImage.PixelHeight / 2.0;
    transformGroup.Children.Add(rotateTransform);
    TranslateTransform translateTransform = new TranslateTransform();
    translateTransform.X = -startX;
    translateTransform.Y = -startY;
    transformGroup.Children.Add(translateTransform);

    DrawingVisual vis = new DrawingVisual();
    DrawingContext cont = vis.RenderOpen();
    cont.PushTransform(transformGroup);
    cont.DrawImage(sourceImage, new Rect(new Size(sourceImage.PixelWidth, sourceImage.PixelHeight)));
    cont.Close();

    RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
    rtb.Render(vis);

    FileStream stream = new FileStream(filePath, FileMode.Create);
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(rtb));
    encoder.Save(stream);
    stream.Close();
}

www.un.org/Depts/DGACM/index_spanish.htm 感谢您担任这一职务,我不得不改变职责范围:。

    private static double RadianToDegree(double angle)
    {
        return angle * (180.0 / Math.PI);
    }

    public static double GetDistanceBetweenPoints(Point p, Point q)
    {
        var a = p.X - q.X;
        var b = p.Y - q.Y;
        return Math.Sqrt(a * a + b * b);
    }

    private static double CalculateTheta(Point p1, Point p2)
    {
        var deltaX = Math.Abs(p1.Y - p2.Y);
        var deltaY = Math.Abs(p1.X - p2.X);
        var theta = RadianToDegree(Math.Atan(deltaY / deltaX));
        return (90 - theta) * -1;
    }

    public void RotateAndSaveImage(string sourceFile, List<Point> subCoords)
    {
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource = new Uri(sourceFile);
        bitmap.EndInit();

        var p1 = subCoords[0];
        var p2 = subCoords[1];
        var p4 = subCoords[3];

        var theta = CalculateTheta(p1, p2);
        var width = GetDistanceBetweenPoints(p1, p2);
        var height = GetDistanceBetweenPoints(p1, p4);

        var transformGroup = new TransformGroup();
        var rotateTransform = new RotateTransform(theta)
        {
            CenterX = p1.X,
            CenterY = p1.Y
        };
        transformGroup.Children.Add(rotateTransform);

        var translateTransform = new TranslateTransform
        {
            X = -p1.X,
            Y = -p1.Y
        };

        transformGroup.Children.Add(translateTransform);

        var vis = new DrawingVisual();

        using (var cont = vis.RenderOpen())
        {
            cont.PushTransform(transformGroup);
            cont.DrawImage(bitmap, new Rect(
                new Size(bitmap.PixelWidth, bitmap.PixelHeight)));
        }

        var rtb = new RenderTargetBitmap((int)width, (int)height,
            96d, 96d, PixelFormats.Default);
        rtb.Render(vis);

        using (var stream = new FileStream(TargetFile, FileMode.Create))
        {
            var encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(stream);
        }
    }

以上法典完全有效。 如果没有你的帮助,我永远不会做到这一点。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签