Изменение разрешения изображения - C#
Формулировка задачи:
Изменяю разрешение изображения с помощью SetResolution, потом сохраняю в Jpeg. В итоге разрешение получается "квадратное", т.е. хотел 1366 на 768,а получил 1366 на 1366. проверял разрешение изображения после SetResolution, там, вроде бы, все правильно.
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap j;
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult rsl = MessageBox.Show("Вы действительно хотите выйти из приложения?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (rsl == DialogResult.Yes)
{
Application.Exit();
}
}
private void bMPToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadImage(false);
}
private void jPEGToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadImage(true);
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
LoadImage(false);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
LoadImage(true);
}
private void LoadImage(bool jpg)
{
openFileDialog1.InitialDirectory = "c:";
if (jpg)
{
openFileDialog1.Filter = "image (JPEG) files (*.jpg)|*.jpg|All files (*.*)|*.*";
}
else
{
openFileDialog1.Filter = "image (BMP) files (*.bmp)|*.bmp|All files (*.*)|*.*";
}
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
j = (Bitmap)Image.FromFile(openFileDialog1.FileName);
pictureBox1.Size = new Size(j.Width, j.Height);
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
catch (Exception ex)
{
MessageBox.Show("Не удалось загрузить файл: " + ex.Message);
}
}
}
private void changeSizeToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((toolStripHeigth.Text == "") & (toolStripWidth.Text == ""))
{
MessageBox.Show("Enter width and heigth of picture!");
}
else
{
Bitmap Image = new Bitmap(j, Convert.ToInt32(toolStripWidth.Text), Convert.ToInt32(toolStripHeigth.Text));
j = Image;
pictureBox1.Size = new Size(Image.Width, Image.Height);
pictureBox1.Image = Image;
}
}
private void changeAllowingToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if ((toolStripTextBoxHeight.Text == "") & (toolStripTextBoxWidth.Text == ""))
{
MessageBox.Show("Enter width and heigth of picture!");
}
else
{
j.SetResolution(Convert.ToInt32(toolStripTextBoxWidth.Text), Convert.ToInt32(toolStripTextBoxHeight.Text));
pictureBox1.Image= j;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Title = "Сохранить картинку как ...";
savedialog.OverwritePrompt = true;
savedialog.CheckPathExists = true;
savedialog.Filter = "Image Files(*.BMP)|*.BMP|Image Files(*.JPG)|*.JPG|All files (*.*)|*.*";
if (savedialog.ShowDialog() == DialogResult.OK)
{
try
{
j.Save(savedialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{
MessageBox.Show("Impossible to save image", "FATAL ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}
private void changeAllowingToolStripMenuItem_Click_1 - изменение разрешения
private void saveToolStripMenuItem_Click -сохранение
Решение задачи: «Изменение разрешения изображения»
textual
Листинг программы
private void changeAllowingToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if ((toolStripTextBoxHeight.Text == "") & (toolStripTextBoxWidth.Text == ""))
{
MessageBox.Show("Enter width and heigth of picture!");
}
else
{
Bitmap Image2 = new Bitmap(j);
Image2.SetResolution(Convert.ToInt32(toolStripTextBoxWidth.Text), Convert.ToInt32(toolStripTextBoxHeight.Text));
j = Image2;
pictureBox1.Image= j;
}
}