c# - 在 C# 中使用图片代替矩形

标签 c# wpf canvas shapes

背景:我目前正忙于根据位置 (X,Y) 和方向(用于旋转)在可缩放 Canvas 上显示车辆的位置。我使用 Rectangle 来可视化车辆。一切正常,但我有点贪心,现在我想用车辆的顶 View 图片替换矩形,所以看起来车辆本身正在移动而不是矩形。

代码如下:

private void PaintLocationVehicle(VehicleClass vc)
{

    IEnumerable<Rectangle> collection = vc.ZoomableCanvas.Children.OfType<Rectangle>().Where(x => x.Name == _vehicleobjectname);
    List<Rectangle> listE = collection.ToList<Rectangle>();
    for (int e = 0; e < listE.Count; e++)
        vc.ZoomableCanvas.Children.Remove(listE[e]);

    // Assign X and Y Position from Vehicle
    double drawingX = vc.gCurrentX * GlobalVar.DrawingQ;
    double drawingY = vc.gCurrentY * GlobalVar.DrawingQ;
    // Scale Length and Width of Vehicle
    double tractorWidthScaled = vc.tractorWidth * GlobalVar.DrawingQ;
    double tractorLengthScaled = vc.tractorLength * GlobalVar.DrawingQ;
    // Get Drawing Location
    double _locationX = drawingX - (tractorLengthScaled / 2);
    double _locationY = drawingY - ((tractorWidthScaled  / 2));
    RotateTransform rotation = new RotateTransform();
    // Angle in 10th of a Degree
    rotation.Angle = vc.gCurrentTheeta/10 ;
    double i = 0;
    //paint the node
    Rectangle _rectangle = new Rectangle();
    _rectangle.Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString(vc.VehicleColor == "" ? "Black" : vc.VehicleColor));
        _rectangle.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(vc.VehicleColor == "" ? "Black" : vc.VehicleColor));
        i += 0;
    _rectangle.Width = tractorLengthScaled ;
    _rectangle.Height = tractorWidthScaled;
    rotation.CenterX = _rectangle.Width / 2;
    rotation.CenterY = _rectangle.Height / 2;
    _rectangle.RenderTransform = rotation;
    Canvas.SetTop(_rectangle, _locationY + i);
    Canvas.SetLeft(_rectangle, _locationX + i);
    _rectangle.SetValue(ZoomableCanvas.ZIndexProperty, 2);
    string _tooltipmsg = "Canvas: " + vc.ZoomableCanvas.Name;
    // Assign ToolTip Values for User
    _tooltipmsg += "\nX: " + vc.gCurrentX;
    _tooltipmsg += "\nY: " + vc.gCurrentY;
    _rectangle.ToolTip = _tooltipmsg;
    _rectangle.Name = _vehicleobjectname;
    //add to the canvas
    vc.ZoomableCanvas.Children.Add(_rectangle);
}

注意:VehicleClass 包含特定车辆的所有值。 DrawingQ 保存从 Reality 到 Zoomable Canvas 的转换比例。 所以我预见的问题:

  1. 如何附加 Jpeg 文件的大小以获得与 矩形?
  2. 我应该使用哪种 Shape 对象?请 建议。

最佳答案

如果我理解正确的话。你想在矩形内显示车辆的图像。为此,您可以使用

ImageBrush and assign to the Rectangle Fill property

像这样的

        Rectangle rect = new Rectangle();
        rect.Width = 100;
        rect.Height = 100;
        ImageBrush img = new ImageBrush();
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.UriSource = new Uri("vehicle image path");
        bmp.EndInit();
        img.ImageSource = bmp;
        rect.Fill = img;

希望对你有帮助

关于c# - 在 C# 中使用图片代替矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263094/

相关文章:

c# - 相当于 WPF 中的 glOrtho

javascript - 如何调整 Canvas 大小以适合浏览器窗口?

javascript - HTML Canvas 游戏 : 2D collision detection

c# - 尝试了对象反序列化和 LINQ 后,无法解析 XML 对象和子对象

c# - 获取程序集的公司名称和版权信息

c# - 如何将字符串拆分为字典

c# windows 控件,显示多线程软件中文件的下载进度状态

wpf - 在样式中定义 InputBindings

c# - RenderTransform 后更新边距

javascript - 插入百分比 charts.js donut