这是我用于旋转的 Form1 代码及其工作图像,我可以将其旋转 360 度。问题是在旋转图像时会损失很多质量。
我尝试在那里使用一些编解码器,但我不确定如何用我的代码实现它。
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.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace AnimatedGifEditor
{
public partial class Form1 : Form
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
Image myImage;
AnimatedGif myGif;
Bitmap bitmap;
public Form1()
{
InitializeComponent();
myImage = Image.FromFile(@"D:\fananimation.gif");
myGif = new AnimatedGif(@"D:\fananimation.gif");
for (int i = 0; i < myGif.Images.Count; i++)
{
pictureBox1.Image = myGif.Images[3].Image;
bitmap = new Bitmap(pictureBox1.Image);
// pictureBox1.Image = bitmap;
}
trackBar1.Maximum = 360;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private Bitmap RotateImage(Bitmap bmp, float angle)
{
Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(rotatedImage))
{
g.TranslateTransform(bmp.Width / 2, bmp.Height / 2); //set the rotation point as the center into the matrix
g.RotateTransform(angle); //rotate
g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2); //restore rotation point into the matrix
g.DrawImage(bmp, new Point(0, 0)); //draw the image on the new bitmap
}
return rotatedImage;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
pictureBox1.Image = RotateImage(bitmap, trackBar1.Value);
}
}
}
在表格的顶部,我添加了:
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
在我添加的滚动事件中:
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
但我从不使用这个编码代码。我该怎么做 ?