gpt4 book ai didi

c# - .NET 在绘制图形时缩放图像

转载 作者:太空狗 更新时间:2023-10-29 23:25:18 25 4
gpt4 key购买 nike

我想制作一个将 png 图像合并为一个 png 的程序。所有图片都是 78px 高,120px 宽,所以我创建了一个 1200x78 的位图(我需要合并 10 张图片)它合并了,但是结果 png 中的每个图像都被缩放了大约 x2。为什么?

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Drawing.Imaging;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
OpenFileDialog openFileDialog1;
Bitmap bitmap;

public Form1()
{
InitializeComponent();
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = true;
openFileDialog1.Filter =
"Images (*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
openFileDialog1.Title = "Select images to merge";

bitmap = new Bitmap(1200, 78);
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();

if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
int shift = 0;
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
try
{
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = loadedImage.Height;
pb.Width = loadedImage.Width;
pb.Image = loadedImage;
flowLayoutPanel2.Controls.Add(pb);

paintToBitmap(loadedImage, shift);

shift += loadedImage.Width;
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error\n\n" +
"Error message: " + ex.Message + "\n\n" +
"Details (send to Support):\n\n" + ex.StackTrace
);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("!!!");
}
}


saveImage();
}
}

private void paintToBitmap(Image image, int shift)
{
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(image, new Point(shift, 0));
}

private void saveImage()
{
bitmap.Save("d:\\result.png", System.Drawing.Imaging.ImageFormat.Png);
}

private void flowLayoutPanel2_Paint(object sender, PaintEventArgs e)
{

}
}
}

最佳答案

尝试更新你的代码

graphics.DrawImage(image, new Rectangle(shift, 0,120,78));

关于c# - .NET 在绘制图形时缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10204928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com