C# 中的图像缩放工具
我正在尝试制作一个轨迹栏,它将放大和缩小图片框中的图片。这是我当前的代码:
namespace Zoom_in_and_Out_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image imgOriginal;
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = Image.FromFile(@"C:\New Folder\picture1.jpg");
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.BackgroundImageLayout = ImageLayout.Stretch;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
// reduce flickering
this.DoubleBuffered = true;
}
public Image PictureBoxZoom(Image img, Size size)
{
Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Graphics grap = Graphics.FromImage(bm);
grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
return bm;
}
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
picBox.Image = null;
picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
}
}
目前它出现了两个问题。一是它确实想使用 grap.InterpolationMode = InterpolationMode.HighQualityBicubic; 行进行编译。 。第二个问题是,当我尝试缩放时,会出现错误:““ArgumentException was unhandled”行错误:Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert .ToInt32(img.Height * size.Height)); " 任何帮助都会很棒,
谢谢
更新 第一个错误表示:“名称‘InterpolationMode’在当前上下文中不存在” 当我注释掉这一行时,第二个错误是:“NullReferenceException 未处理”“对象引用未设置为对象的实例”。在线 Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
谢谢
I am trying to make a trackbar which will zoom in and out on a picture in a picturebox. This is my current code:
namespace Zoom_in_and_Out_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image imgOriginal;
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = Image.FromFile(@"C:\New Folder\picture1.jpg");
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.BackgroundImageLayout = ImageLayout.Stretch;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
// reduce flickering
this.DoubleBuffered = true;
}
public Image PictureBoxZoom(Image img, Size size)
{
Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Graphics grap = Graphics.FromImage(bm);
grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
return bm;
}
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
picBox.Image = null;
picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
}
}
Currently it comes up with 2 problems. One being it does want to compile with the line grap.InterpolationMode = InterpolationMode.HighQualityBicubic; . The Second problem is that when i try to zoom it comes up with the error: " "ArgumentException was unhandled" error at the line: Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height)); " Any help would be great,
Thanks
UPDATE
The first error says: "The name 'InterpolationMode' does not exist in the current context"
The second error when i comment this line out is: 'NullReferenceException was unhandled "Object reference not set to an instance of an object.' on the line Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包含
在您的使用列表中。
第二个错误可能是由于
img
为空或size
为空造成的。Include
in your using list.
The second error could be due to either the
img
being null or thesize
being null.第一个编译器错误很可能是由对
InterpolationMode.HighQualityBicubic
的未知引用引起的。InterpolationMode
枚举位于Drawing2D
命名空间中,该命名空间是System.Drawing
的子命名空间。您可以通过为
System.Drawing.Drawing2D
添加额外的Using
指令,或者完全限定代码中的命名空间来修复此错误:您的代码的第二问题是您指定为此方法的参数的图像 (
img
) 是空引用。一旦创建控件(在表单的构造函数中),缩放滑块的Scroll
事件可能就会引发,这是在表单的Load
方法中的代码之前run,这就是创建图像的过程(通过从磁盘上的文件加载图像)。尝试向
Scroll
事件处理程序添加 null 检查:最后,我注意到您正在设置图片框的
BackgroundImageLayout
属性,但您发布的代码实际上都没有指定图片框的背景图像。您是否想设置SizeMode< /code> 属性
来调整图像的显示方式?像这样的东西:
The first compiler error is more than likely caused by an unknown reference to
InterpolationMode.HighQualityBicubic
. TheInterpolationMode
enumeration is found in theDrawing2D
namespace, which is a child namespace ofSystem.Drawing
.You can fix this error either by adding an additional
Using
directive forSystem.Drawing.Drawing2D
, or by fully qualifying the namespace in your code:The second problem with your code is that the image you're specifying as a parameter to this method (
img
) is a null reference. TheScroll
event of your zoom slider is probably getting raised as soon as the control is created (in your form's constructor), which is before the code in your form'sLoad
method is run, which is what creates the image (by loading it from a file on disk).Try adding a null check to the
Scroll
event handler:Finally, I noticed that you're setting the
BackgroundImageLayout
property of the picture box, but none of the code you post is actually specifying a background image for the picture box. Did you mean to set theSizeMode
property to adjust how the image is displayed? Something like: