如何在窗体背景上将一幅矩形图片拉伸绘制成梯形?

victorycy 2007-07-07 10:16:10
如题.

谢谢回复.
...全文
1339 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
victorycy 2007-07-10
  • 打赏
  • 举报
回复
那要用到D3D就算了,不去验证了. 结帖.
兔子-顾问 2007-07-07
  • 打赏
  • 举报
回复
private void Form8_Paint(object sender, PaintEventArgs e)
{
// 创建一个全部由线条组成的路径
PointF[] points = new PointF[]
{
new PointF(20.0f, 60.0f),
new PointF(30.0f, 90.0f),
new PointF(15.0f, 110.0f),
new PointF(15.0f, 145.0f),
new PointF(55.0f, 145.0f),
new PointF(55.0f, 110.0f),
new PointF(40.0f, 90.0f),
new PointF(50.0f, 60.0f)
};

GraphicsPath path = new GraphicsPath();
path.AddLines(points);
path.CloseFigure();

// 绘制扭曲前的路径
Pen bluePen = new Pen(Color.Blue, 3);
e.Graphics.DrawPath(bluePen, path);

// 定义放置路径的源矩形
RectangleF srcRect = new RectangleF(10.0f, 50.0f, 50.0f, 100.0f);
//定义矩形映射的目标点
PointF[] destPts = new PointF[]
{
new PointF(200.0f, 10.0f),
new PointF(250.0f, 10.0f),
new PointF(100.0f, 150.0f),
new PointF(300.0f, 150.0f)
};
//扭曲矩形
path.Warp(destPts, srcRect);
// 绘制扭曲后的路径
e.Graphics.DrawPath(bluePen, path);

// 绘制源矩形和目标的多边形
Pen blackPen = new Pen(Color.Black, 1);
e.Graphics.DrawRectangle(blackPen,
srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height);
e.Graphics.DrawLine(blackPen, destPts[0], destPts[1]);
e.Graphics.DrawLine(blackPen, destPts[0], destPts[2]);
e.Graphics.DrawLine(blackPen, destPts[1], destPts[3]);
e.Graphics.DrawLine(blackPen, destPts[2], destPts[3]);
}
victorycy 2007-07-07
  • 打赏
  • 举报
回复
难道就没人能给几行代码瞧瞧?
vrhero 2007-07-07
  • 打赏
  • 举报
回复
GDI+...自己画...拉是拉不出来的...
taotingzhong 2007-07-07
  • 打赏
  • 举报
回复
GDI+
你看看这方面的知识;好好研究一下!
victorycy 2007-07-07
  • 打赏
  • 举报
回复
nic7968(得不到的往往是最差), 老兄真是惜字如金啊.
nic7968 2007-07-07
  • 打赏
  • 举报
回复
GDI+
e_fighter 2007-07-07
  • 打赏
  • 举报
回复
转换函数用d3d可以调用点现成的,用gdi+的话就需要自己写,都需要些图形学的的知识,没有时间啊,不然写一个练练手也好
blackant2 2007-07-07
  • 打赏
  • 举报
回复
我想通过矩阵转换可以实现拉伸,扭曲等各种效果,可怜我当年没好好上学
下面的例子改自深入managed directx3d,只是一个示范
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;
using System.Threading;


namespace WrapRectImage
{
public class Form1 : Form
{

private Device device = null;
private VertexBuffer vb = null;
private Texture tex = null;

public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}


public void InitializeGraphics()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 36, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);

tex = new Texture(device, new Bitmap("../../test.bmp"), Usage.Dynamic, Pool.Default);
}

private void OnVertexBufferCreate(object sender, object p)
{
VertexBuffer buffer = (VertexBuffer)sender;

CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[6];
verts[0] = new CustomVertex.PositionTextured(-1f, 1.0f, 0.0f, 0.0f, 0.0f);
verts[1] = new CustomVertex.PositionTextured(-1f, -1.0f, 0.0f, 0.0f, 1.0f);
verts[2] = new CustomVertex.PositionTextured(1f, 1.0f, 0.0f, 1.0f, 0.0f);
verts[3] = new CustomVertex.PositionTextured(-1f, -1.0f, 0.0f, 0.0f, 1.0f);
verts[4] = new CustomVertex.PositionTextured(1f, -1.0f, 0.0f, 1.0f, 1.0f);
verts[5] = new CustomVertex.PositionTextured(1f, 1.0f, 0.0f, 1.0f, 0.0f);
buffer.SetData(verts, 0, LockFlags.None);
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;



#region Windows Form Designer generated code
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}



/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
#endregion


protected override void OnPaint(PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);


device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
//调整相机的位置,可以取各种角度变形
//当然,计算好了位置和角度再来拍才是你想要的结果
Random ra = new Random();
float x = ra.Next(10) - 5;
float y = ra.Next(10) - 5;
float z = 3;

Vector3 cameraPosition = new Vector3(x, y, z);
device.Transform.View = Matrix.LookAtLH(cameraPosition, new Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;

device.BeginScene();
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.SetStreamSource(0, vb, 0);

device.Transform.World = Matrix.RotationYawPitchRoll(0,0 , 0) * Matrix.Translation(0f, 0f, 0f);
device.SetTexture(0,tex );
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);


device.EndScene();

device.Present();
Thread.Sleep(1000);
this.Invalidate();

}
}
}
victorycy 2007-07-07
  • 打赏
  • 举报
回复

wuyazhe,谢谢提供代码.

我要位伸的是图片,不是矢量图形. 好象Wrap方法不能应用于图片吧.

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧