Tôi muốn tạo một chương trình kết hợp các hình ảnh png thành một png. Tất cả các hình ảnh có chiều cao 78px, chiều rộng 120px, vì vậy tôi đã tạo một bitmap 1200x78 (tôi cần hợp nhất 10 hình ảnh) Nó kết hợp, nhưng mọi hình ảnh trong png kết quả được chia tỷ lệ khoảng x2. Tại sao?Hình ảnh tỷ lệ .NET khi vẽ đến đồ họa
Đây là mã của tôi:
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)
{
}
}
}
tôi chạy mã của bạn và nó có vẻ hoạt động tốt. Bạn có thể đính kèm một trong những hình ảnh đầu vào của bạn? Tôi đoán rằng đây là một vấn đề với hình ảnh của bạn liên quan đến dpi hoặc như nhau. – Till
http://files.moonmana.com/forums/m1.png http://files.moonmana.com/forums/m2.png –