写一个小程序,程序运行后背景是白屏(满屏),然后可以将图片导入到白屏,在白屏里对图片可以伸缩,移动 想写一个小程序,程序运行后背景是白屏(满屏),然后可以将图片导入到白屏,在白屏里对图片可以伸缩,移动没有思路,给思路,代码都非常感谢。 VC, C#代码都可以 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以下代码,你新建个项目Test,修改Form1为TestForm 然后打开TestForm.Designer.cs和TestForm.cs替换里面的代码即可.鼠标滚动,可以放大缩小.鼠标可以拖动图片.如果满意就给100分吧.//TestForm.Designer.csnamespace Test{ partial class TestForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Pic_Image = new System.Windows.Forms.PictureBox(); this.Btn_Open = new System.Windows.Forms.Button(); this.Ofd_ImageFile = new System.Windows.Forms.OpenFileDialog(); this.Hsl_Scaling = new System.Windows.Forms.HScrollBar(); this.Pan_Main = new System.Windows.Forms.Panel(); this.Lab_ImagePath = new System.Windows.Forms.Label(); this.Txt_Path = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.Pic_Image)).BeginInit(); this.Pan_Main.SuspendLayout(); this.SuspendLayout(); // // Pic_Image // this.Pic_Image.Anchor = System.Windows.Forms.AnchorStyles.None; this.Pic_Image.Cursor = System.Windows.Forms.Cursors.SizeAll; this.Pic_Image.Location = new System.Drawing.Point(0, 0); this.Pic_Image.Name = "Pic_Image"; this.Pic_Image.Size = new System.Drawing.Size(406, 261); this.Pic_Image.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; this.Pic_Image.TabIndex = 0; this.Pic_Image.TabStop = false; this.Pic_Image.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseMove); this.Pic_Image.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseDown); this.Pic_Image.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseUp); // // Btn_Open // this.Btn_Open.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.Btn_Open.Location = new System.Drawing.Point(343, 10); this.Btn_Open.Name = "Btn_Open"; this.Btn_Open.Size = new System.Drawing.Size(75, 23); this.Btn_Open.TabIndex = 4; this.Btn_Open.Text = "打开"; this.Btn_Open.UseVisualStyleBackColor = true; this.Btn_Open.Click += new System.EventHandler(this.Btn_Open_Click); // // Ofd_ImageFile // this.Ofd_ImageFile.FileName = "*.*"; this.Ofd_ImageFile.Filter = "BMP 图像|*.bmp|JPEG 图像|*.jpg;*.jpeg|GIF 图像|*.gif|PNG 图像|*.png|所有图像文件|*.*"; // // Hsl_Scaling // this.Hsl_Scaling.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.Hsl_Scaling.Location = new System.Drawing.Point(12, 304); this.Hsl_Scaling.Maximum = 16; this.Hsl_Scaling.Minimum = 1; this.Hsl_Scaling.Name = "Hsl_Scaling"; this.Hsl_Scaling.Size = new System.Drawing.Size(406, 17); this.Hsl_Scaling.TabIndex = 5; this.Hsl_Scaling.Value = 1; this.Hsl_Scaling.ValueChanged += new System.EventHandler(this.Hsl_Scaling_ValueChanged); // // Pan_Main // this.Pan_Main.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.Pan_Main.Controls.Add(this.Pic_Image); this.Pan_Main.Location = new System.Drawing.Point(12, 40); this.Pan_Main.Name = "Pan_Main"; this.Pan_Main.Size = new System.Drawing.Size(406, 261); this.Pan_Main.TabIndex = 7; // // Lab_ImagePath // this.Lab_ImagePath.AutoSize = true; this.Lab_ImagePath.Location = new System.Drawing.Point(12, 15); this.Lab_ImagePath.Name = "Lab_ImagePath"; this.Lab_ImagePath.Size = new System.Drawing.Size(59, 12); this.Lab_ImagePath.TabIndex = 8; this.Lab_ImagePath.Text = "图片路径:"; // // Txt_Path // this.Txt_Path.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.Txt_Path.Location = new System.Drawing.Point(77, 12); this.Txt_Path.Name = "Txt_Path"; this.Txt_Path.Size = new System.Drawing.Size(260, 21); this.Txt_Path.TabIndex = 9; // // TestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(430, 337); this.Controls.Add(this.Txt_Path); this.Controls.Add(this.Lab_ImagePath); this.Controls.Add(this.Hsl_Scaling); this.Controls.Add(this.Btn_Open); this.Controls.Add(this.Pan_Main); this.MinimumSize = new System.Drawing.Size(446, 375); this.Name = "TestForm"; this.Text = "TestForm"; this.Load += new System.EventHandler(this.TestForm_Load); ((System.ComponentModel.ISupportInitialize)(this.Pic_Image)).EndInit(); this.Pan_Main.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.PictureBox Pic_Image; private System.Windows.Forms.Button Btn_Open; private System.Windows.Forms.OpenFileDialog Ofd_ImageFile; private System.Windows.Forms.HScrollBar Hsl_Scaling; private System.Windows.Forms.Panel Pan_Main; private System.Windows.Forms.Label Lab_ImagePath; private System.Windows.Forms.TextBox Txt_Path; }} //TestForm.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Drawing.Imaging;using System.Drawing.Drawing2D;namespace Test{ public partial class TestForm : Form { public TestForm() { InitializeComponent(); } private Image Src = null; bool M_Down = false; Point M_Location = Point.Empty; private void Btn_Open_Click(object sender, EventArgs e) { if (this.Ofd_ImageFile.ShowDialog(this) == DialogResult.OK) { this.Txt_Path.Text = this.Ofd_ImageFile.FileName; this.Pic_Image.Image = Src = Image.FromFile(this.Ofd_ImageFile.FileName); } } private void TestForm_Load(object sender, EventArgs e) { this.MouseWheel += new MouseEventHandler(TestForm_MouseWheel); } void TestForm_MouseWheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousePoint = this.PointToScreen(new Point(e.X, e.Y)); //判断是否在panel内 if (this.Pan_Main.RectangleToScreen(Pan_Main.DisplayRectangle).Contains(mousePoint)) { //滚动 int value = this.Hsl_Scaling.Value + e.Delta / 120; if (value <= this.Hsl_Scaling.Maximum && value >= this.Hsl_Scaling.Minimum) { this.Hsl_Scaling.Value = value; } } } private void Hsl_Scaling_ValueChanged(object sender, EventArgs e) { if (Src != null) { Size s = this.Pic_Image.Size; this.Pic_Image.Size = new Size(Src.Width * this.Hsl_Scaling.Value, Src.Height * this.Hsl_Scaling.Value); Bitmap bmp = new Bitmap(this.Pic_Image.Width, this.Pic_Image.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = InterpolationMode.NearestNeighbor; g.DrawImage(Src, 0, 0, bmp.Width, bmp.Height); } this.Pic_Image.Image = (Image)bmp.Clone(); if (s.Width > this.Pic_Image.Size.Width) { //缩小 this.Pic_Image.Location = new Point(this.Pic_Image.Left + (s.Width - this.Pic_Image.Size.Width) / 2, this.Pic_Image.Top + (s.Height - this.Pic_Image.Size.Height) / 2); } else if (s.Width < this.Pic_Image.Size.Width) { //放大 this.Pic_Image.Location = new Point(this.Pic_Image.Left - (this.Pic_Image.Size.Width - s.Width) / 2, this.Pic_Image.Top - (this.Pic_Image.Size.Height - s.Height) / 2); } } } private void Btn_Src_Click(object sender, EventArgs e) { if (this.Hsl_Scaling.Value == 1) { Hsl_Scaling_ValueChanged(sender, e); } else { this.Hsl_Scaling.Value = 1; } } private void Pic_Image_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { M_Down = true; this.M_Location = new Point(e.X, e.Y); } } private void Pic_Image_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { M_Down = false; } } private void Pic_Image_MouseMove(object sender, MouseEventArgs e) { if (M_Down && e.Button == MouseButtons.Left) { if (M_Down) { this.Pic_Image.Location = new Point(this.Pic_Image.Location.X + e.X - M_Location.X, this.Pic_Image.Location.Y + e.Y - M_Location.Y); } } } }} 谢谢sjzlxd, 你的程序对我很有帮助,我想对我有的帮助远超100分。还想请教下就是怎么在保持背景不变的情况下调整图片的尺寸? 导入多个图片的时候不要覆盖以前的图片?就像Word一样,可以导入多个图片,可以随意改变图片的形状。 如果想导入多个 不覆盖 就需要多个pic控件了. 这个说白了就是分层了. 跟Photoshop的图片 层 是一个道理. 求助!DataGridView使用问题! C#函数中如何返回多个返回值? 如何用C#创建数字证书(注意:是把自己的密钥生成程序生成的公钥放到证书中) 关于remoting的问题,很简单 datagridview 选中某个单元格的时候,此单元格在显示原来的文本信息的同时,后面多了按钮 控件ListView第一个字段无法显示,求教高手! double类型的数据小数点后面位数太长,怎么样才能保留两位小数呀? html转word 如何替换同名文件? 关闭释放 这个pdfCtl 对象 C#反射问题,不知道可不可以实现这样的问题! 提取数据库中每分钟内第一个时间点的数据
然后打开TestForm.Designer.cs和TestForm.cs
替换里面的代码即可.
鼠标滚动,可以放大缩小.鼠标可以拖动图片.如果满意就给100分吧.
//TestForm.Designer.cs
namespace Test
{
partial class TestForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Pic_Image = new System.Windows.Forms.PictureBox();
this.Btn_Open = new System.Windows.Forms.Button();
this.Ofd_ImageFile = new System.Windows.Forms.OpenFileDialog();
this.Hsl_Scaling = new System.Windows.Forms.HScrollBar();
this.Pan_Main = new System.Windows.Forms.Panel();
this.Lab_ImagePath = new System.Windows.Forms.Label();
this.Txt_Path = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.Pic_Image)).BeginInit();
this.Pan_Main.SuspendLayout();
this.SuspendLayout();
//
// Pic_Image
//
this.Pic_Image.Anchor = System.Windows.Forms.AnchorStyles.None;
this.Pic_Image.Cursor = System.Windows.Forms.Cursors.SizeAll;
this.Pic_Image.Location = new System.Drawing.Point(0, 0);
this.Pic_Image.Name = "Pic_Image";
this.Pic_Image.Size = new System.Drawing.Size(406, 261);
this.Pic_Image.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.Pic_Image.TabIndex = 0;
this.Pic_Image.TabStop = false;
this.Pic_Image.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseMove);
this.Pic_Image.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseDown);
this.Pic_Image.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Pic_Image_MouseUp);
//
// Btn_Open
//
this.Btn_Open.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Btn_Open.Location = new System.Drawing.Point(343, 10);
this.Btn_Open.Name = "Btn_Open";
this.Btn_Open.Size = new System.Drawing.Size(75, 23);
this.Btn_Open.TabIndex = 4;
this.Btn_Open.Text = "打开";
this.Btn_Open.UseVisualStyleBackColor = true;
this.Btn_Open.Click += new System.EventHandler(this.Btn_Open_Click);
//
// Ofd_ImageFile
//
this.Ofd_ImageFile.FileName = "*.*";
this.Ofd_ImageFile.Filter = "BMP 图像|*.bmp|JPEG 图像|*.jpg;*.jpeg|GIF 图像|*.gif|PNG 图像|*.png|所有图像文件|*.*";
//
// Hsl_Scaling
//
this.Hsl_Scaling.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.Hsl_Scaling.Location = new System.Drawing.Point(12, 304);
this.Hsl_Scaling.Maximum = 16;
this.Hsl_Scaling.Minimum = 1;
this.Hsl_Scaling.Name = "Hsl_Scaling";
this.Hsl_Scaling.Size = new System.Drawing.Size(406, 17);
this.Hsl_Scaling.TabIndex = 5;
this.Hsl_Scaling.Value = 1;
this.Hsl_Scaling.ValueChanged += new System.EventHandler(this.Hsl_Scaling_ValueChanged);
//
// Pan_Main
//
this.Pan_Main.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.Pan_Main.Controls.Add(this.Pic_Image);
this.Pan_Main.Location = new System.Drawing.Point(12, 40);
this.Pan_Main.Name = "Pan_Main";
this.Pan_Main.Size = new System.Drawing.Size(406, 261);
this.Pan_Main.TabIndex = 7;
//
// Lab_ImagePath
//
this.Lab_ImagePath.AutoSize = true;
this.Lab_ImagePath.Location = new System.Drawing.Point(12, 15);
this.Lab_ImagePath.Name = "Lab_ImagePath";
this.Lab_ImagePath.Size = new System.Drawing.Size(59, 12);
this.Lab_ImagePath.TabIndex = 8;
this.Lab_ImagePath.Text = "图片路径:";
//
// Txt_Path
//
this.Txt_Path.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.Txt_Path.Location = new System.Drawing.Point(77, 12);
this.Txt_Path.Name = "Txt_Path";
this.Txt_Path.Size = new System.Drawing.Size(260, 21);
this.Txt_Path.TabIndex = 9;
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(430, 337);
this.Controls.Add(this.Txt_Path);
this.Controls.Add(this.Lab_ImagePath);
this.Controls.Add(this.Hsl_Scaling);
this.Controls.Add(this.Btn_Open);
this.Controls.Add(this.Pan_Main);
this.MinimumSize = new System.Drawing.Size(446, 375);
this.Name = "TestForm";
this.Text = "TestForm";
this.Load += new System.EventHandler(this.TestForm_Load);
((System.ComponentModel.ISupportInitialize)(this.Pic_Image)).EndInit();
this.Pan_Main.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.PictureBox Pic_Image;
private System.Windows.Forms.Button Btn_Open;
private System.Windows.Forms.OpenFileDialog Ofd_ImageFile;
private System.Windows.Forms.HScrollBar Hsl_Scaling;
private System.Windows.Forms.Panel Pan_Main;
private System.Windows.Forms.Label Lab_ImagePath;
private System.Windows.Forms.TextBox Txt_Path;
}
}
//TestForm.csusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;namespace Test
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
} private Image Src = null; bool M_Down = false;
Point M_Location = Point.Empty; private void Btn_Open_Click(object sender, EventArgs e)
{
if (this.Ofd_ImageFile.ShowDialog(this) == DialogResult.OK)
{
this.Txt_Path.Text = this.Ofd_ImageFile.FileName;
this.Pic_Image.Image = Src = Image.FromFile(this.Ofd_ImageFile.FileName);
}
} private void TestForm_Load(object sender, EventArgs e)
{
this.MouseWheel += new MouseEventHandler(TestForm_MouseWheel);
} void TestForm_MouseWheel(object sender, MouseEventArgs e)
{
//获取光标位置
Point mousePoint = this.PointToScreen(new Point(e.X, e.Y));
//判断是否在panel内
if (this.Pan_Main.RectangleToScreen(Pan_Main.DisplayRectangle).Contains(mousePoint))
{
//滚动
int value = this.Hsl_Scaling.Value + e.Delta / 120;
if (value <= this.Hsl_Scaling.Maximum && value >= this.Hsl_Scaling.Minimum)
{
this.Hsl_Scaling.Value = value;
}
}
} private void Hsl_Scaling_ValueChanged(object sender, EventArgs e)
{
if (Src != null)
{
Size s = this.Pic_Image.Size; this.Pic_Image.Size = new Size(Src.Width * this.Hsl_Scaling.Value, Src.Height * this.Hsl_Scaling.Value);
Bitmap bmp = new Bitmap(this.Pic_Image.Width, this.Pic_Image.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(Src, 0, 0, bmp.Width, bmp.Height);
}
this.Pic_Image.Image = (Image)bmp.Clone(); if (s.Width > this.Pic_Image.Size.Width)
{
//缩小
this.Pic_Image.Location = new Point(this.Pic_Image.Left + (s.Width - this.Pic_Image.Size.Width) / 2, this.Pic_Image.Top + (s.Height - this.Pic_Image.Size.Height) / 2);
}
else if (s.Width < this.Pic_Image.Size.Width)
{
//放大
this.Pic_Image.Location = new Point(this.Pic_Image.Left - (this.Pic_Image.Size.Width - s.Width) / 2, this.Pic_Image.Top - (this.Pic_Image.Size.Height - s.Height) / 2);
}
}
} private void Btn_Src_Click(object sender, EventArgs e)
{
if (this.Hsl_Scaling.Value == 1)
{
Hsl_Scaling_ValueChanged(sender, e);
}
else
{
this.Hsl_Scaling.Value = 1;
}
} private void Pic_Image_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
M_Down = true;
this.M_Location = new Point(e.X, e.Y);
}
} private void Pic_Image_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
M_Down = false;
}
} private void Pic_Image_MouseMove(object sender, MouseEventArgs e)
{
if (M_Down && e.Button == MouseButtons.Left)
{
if (M_Down)
{
this.Pic_Image.Location = new Point(this.Pic_Image.Location.X + e.X - M_Location.X, this.Pic_Image.Location.Y + e.Y - M_Location.Y);
}
}
}
}
}