C#實現(xiàn)3D效果完整實例
本文實例講述了C#實現(xiàn)3D效果的方法。分享給大家供大家參考,具體如下:
一、新建一類文件
private static double[] addVector(double[] a, double[] b)
{
return new double[] { a[0] + b[0], a[1] + b[1], a[2] + b[2] };
}
private static double[] scalarProduct(double[] vector, double scalar)
{
return new double[] { vector[0] * scalar, vector[1] * scalar, vector[2] * scalar };
}
private static double dotProduct(double[] a, double[] b)
{
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
private static double norm(double[] vector)
{
return Math.Sqrt(dotProduct(vector, vector));
}
private static double[] normalize(double[] vector)
{
return scalarProduct(vector, 1.0 / norm(vector));
}
private static double[] crossProduct(double[] a, double[] b)
{
return new double[]
{
(a[1] * b[2] - a[2] * b[1]),
(a[2] * b[0] - a[0] * b[2]),
(a[0] * b[1] - a[1] * b[0])
};
}
private static double[] vectorProductIndexed(double[] v, double[] m, int i)
{
return new double[]
{
v[i + 0] * m[0] + v[i + 1] * m[4] + v[i + 2] * m[8] + v[i + 3] * m[12],
v[i + 0] * m[1] + v[i + 1] * m[5] + v[i + 2] * m[9] + v[i + 3] * m[13],
v[i + 0] * m[2] + v[i + 1] * m[6] + v[i + 2] * m[10]+ v[i + 3] * m[14],
v[i + 0] * m[3] + v[i + 1] * m[7] + v[i + 2] * m[11]+ v[i + 3] * m[15]
};
}
private static double[] vectorProduct(double[] v, double[] m)
{
return vectorProductIndexed(v, m, 0);
}
private static double[] matrixProduct(double[] a, double[] b)
{
double[] o1 = vectorProductIndexed(a, b, 0);
double[] o2 = vectorProductIndexed(a, b, 4);
double[] o3 = vectorProductIndexed(a, b, 8);
double[] o4 = vectorProductIndexed(a, b, 12);
return new double[]
{
o1[0], o1[1], o1[2], o1[3],
o2[0], o2[1], o2[2], o2[3],
o3[0], o3[1], o3[2], o3[3],
o4[0], o4[1], o4[2], o4[3]
};
}
private static double[] cameraTransform(double[] C, double[] A)
{
double[] w = normalize(addVector(C, scalarProduct(A, -1)));
double[] y = new double[] { 0, 1, 0 };
double[] u = normalize(crossProduct(y, w));
double[] v = crossProduct(w, u);
double[] t = scalarProduct(C, -1);
return new double[]
{
u[0], v[0], w[0], 0,
u[1], v[1], w[1], 0,
u[2], v[2], w[2], 0,
dotProduct(u, t), dotProduct(v, t), dotProduct(w, t), 1
};
}
private static double[] viewingTransform(double fov, double n, double f)
{
fov *= (Math.PI / 180);
double cot = 1.0 / Math.Tan(fov / 2);
return new double[] { cot, 0, 0, 0, 0, cot, 0, 0, 0, 0, (f + n) / (f - n), -1, 0, 0, 2 * f * n / (f - n), 0 };
}
public static Image Generate(string captchaText)
{
int fontsize = 24;
Font font = new Font("Arial", fontsize);
SizeF sizeF;
using (Graphics g = Graphics.FromImage(new Bitmap(1, 1)))
{
sizeF = g.MeasureString(captchaText, font, 0, StringFormat.GenericDefault);
}
int image2d_x = (int)sizeF.Width;
int image2d_y = (int)(fontsize * 1.3);
Bitmap image2d = new Bitmap(image2d_x, image2d_y);
Color black = Color.Black;
Color white = Color.White;
using (Graphics g = Graphics.FromImage(image2d))
{
g.Clear(black);
g.DrawString(captchaText, font, Brushes.White, 0, 0);
}
Random rnd = new Random();
double[] T = cameraTransform(new double[] { rnd.Next(-90, 90), -200, rnd.Next(150, 250) }, new double[] { 0, 0, 0 });
T = matrixProduct(T, viewingTransform(60, 300, 3000));
double[][] coord = new double[image2d_x * image2d_y][];
int count = 0;
for (int y = 0; y < image2d_y; y += 2)
{
for (int x = 0; x < image2d_x; x++)
{
int xc = x - image2d_x / 2;
int zc = y - image2d_y / 2;
double yc = -(double)(image2d.GetPixel(x, y).ToArgb() & 0xff) / 256 * 4;
double[] xyz = new double[] { xc, yc, zc, 1 };
xyz = vectorProduct(xyz, T);
coord[count] = xyz;
count++;
}
}
int image3d_x = 256;
int image3d_y = image3d_x * 9 / 16;
Bitmap image3d = new Bitmap(image3d_x, image3d_y);
Color fgcolor = Color.White;
Color bgcolor = Color.Black;
using (Graphics g = Graphics.FromImage(image3d))
{
g.Clear(bgcolor);
count = 0;
double scale = 1.75 - (double)image2d_x / 400;
for (int y = 0; y < image2d_y; y += 2)
{
for (int x = 0; x < image2d_x; x++)
{
if (x > 0)
{
double x0 = coord[count - 1][0] * scale + image3d_x / 2;
double y0 = coord[count - 1][1] * scale + image3d_y / 2;
double x1 = coord[count][0] * scale + image3d_x / 2;
double y1 = coord[count][1] * scale + image3d_y / 2;
g.DrawLine(new Pen(fgcolor), (float)x0, (float)y0, (float)x1, (float)y1);
}
count++;
}
}
}
return image3d;
}
注意引用命名空間:
using System.Drawing;
二、頁面調(diào)用
Response.ContentType = "image/pjpeg";
Captcha.Generate("我就是3D內(nèi)容").Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法


