生成相片的程序代码取决于您想要使用的编程语言和具体需求。以下是几种不同编程语言中生成相片的示例代码:
Python 使用PIL库
```python
from PIL import Image, ImageDraw
创建一个新的图片,大小为200x200,背景为白色
image = Image.new('RGB', (200, 200), (255, 255, 255))
创建一个画布
draw = ImageDraw.Draw(image)
在画布上绘制一个红色的矩形
draw.rectangle([(50, 50), (150, 150)], fill='red')
保存图片
image.save('output.png')
```
PHP 使用GD库
```php
if(isset($gd) && $gd==1 && $txt) {
header ("Content-type: image/png");
$txt = urldecode(phpcms_auth($txt, 'DECODE', AUTH_KEY));
$imageX = strlen($txt)*9;
$im = @imagecreate ($imageX, 16) or die ("Cannot Initialize new GD image stream");
$bgColor = ImageColorAllocate($im,255,255,255);
$white=imagecolorallocate($im,234,185,95);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
preg_match('/([a-z0-9@.])/',$txt)) {
$txt = iconv("GB2312","UTF-8",$txt);
ImageTTFText($im, 10, 0, 0, 12, $font_color, $fontfile, $txt);
} else {
$fonttype = intval($fonttype);
imagestring ($im, $fonttype, 0, 0,$txt, $font_color);
}
imagepng ($im);
}
```
C 使用System.Drawing
```csharp
using System.Drawing;
public partial class Pub_validcode : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
this.CreateCheckCodeImage(GenerateCheckCode());
}
// 生成随机数
private string GenerateCheckCode() {
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
// 生成4位随机数
for (int i = 0; i < 4; i++) {
number = random.Next(10);
code = (char)('0' + number);
checkCode += code;
}
return checkCode;
}
protected void CreateCheckCodeImage(string checkCode) {
int width = 150;
int height = 30;
using (Bitmap imgOutput = new Bitmap(width, height, PixelFormats.Format24bppRgb)) {
using (Graphics g = Graphics.FromImage(imgOutput)) {
g.Clear(Color.Yellow);
g.DrawString(checkCode, new Font("黑体", 14, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(2, 2));
}
response.ContentType = "image/gif";
imgOutput.Save(response.OutputStream, ImageFormat.Gif);
}
}
}
```
C++ 使用BMP格式