我有這樣的功能:如何將圖像更改爲與pictureBox相同的大小?
public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
{
try
{
Bitmap b = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage((Image)b))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
}
return b;
}
catch
{
throw;
}
}
但我怎麼用它來放什麼尺寸的大小? 我試圖調用像:ResizeImage(位圖,新尺寸(100,100));但是,那不是路。
在Form1我所做的:
bitmap = new Bitmap(@"D:\ffmpegtorun\ffmpeg-20130509-git-13cb6ed-win32-static\bin\Screenshots\Screenshot000000.jpg");
pictureBox1.Image = bitmap;
因爲一切都在PictureBox中模糊也是,如果是變焦模式或Stretchimage模式我想將圖像尺寸調整到圖片框的大小,也許改變PictureBox的模式正常或延長圖像?我不知道如何解決這個模糊的pictureBox。
我在這裏添加了我的放大鏡玻璃的屏幕截圖,它工作良好,但移動過圖片框時只顯示模糊。
這是當它在Stretchimage模式,但它是相同的,當它也設置爲變焦模式:
https://skydrive.live.com/redir?resid=EB1C71C44C3976D5!269&authkey=!AHI8lMsnZHfka60
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
namespace Magnifier20070401
{
public partial class MagnifierForm : Form
{
private bool _doMove;
public MagnifierForm()
{
InitializeComponent();
_doMove = true;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
Width = 150;
Height = 150;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mTimer = new Timer();
mTimer.Enabled = true;
mTimer.Interval = 20;
mTimer.Tick += new EventHandler(HandleTimer);
mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
mStartPoint = new Point(500, 500);
mTargetPoint = new Point(500, 500);
speed = 0.35F;
zoom = 3.0F;
hidecursor = false;
hue = 30;
}
public MagnifierForm(bool MoveTheGlass, bool InTaskBar, int MagnifierWidth, int MagnifierHeight, Point MagnifierStartPoint, float SpeedFactor, float ChangeZoom, bool HideMouseCursor, float AdjustHue)
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mTimer = new Timer();
mTimer.Enabled = true;
mTimer.Interval = 20;
mTimer.Tick += new EventHandler(HandleTimer);
mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
_doMove = MoveTheGlass;
ShowInTaskbar = InTaskBar;
Width = MagnifierWidth;
Height = MagnifierHeight;
mStartPoint = MagnifierStartPoint;
mTargetPoint = MagnifierStartPoint;
speed = SpeedFactor;
zoom = ChangeZoom;
hidecursor = HideMouseCursor;
hue = AdjustHue;
}
protected override void OnShown(EventArgs e)
{
RepositionAndShow();
}
private delegate void RepositionAndShowDelegate();
private void RepositionAndShow()
{
if (InvokeRequired)
{
Invoke(new RepositionAndShowDelegate(RepositionAndShow));
}
else
{
Graphics g = Graphics.FromImage(mScreenImage);
g.CopyFromScreen(0, 0, 0, 0, new Size(mScreenImage.Width, mScreenImage.Height));
HSLAdjust.BitmapFunctions bf = new HSLAdjust.BitmapFunctions((Bitmap)mScreenImage);
bf.Hue(hue);
bf.Dispose();
g.Dispose();
if (hidecursor == true)
{
Cursor.Hide();
}
else
{
Cursor = Cursors.Cross;
}
Capture = true;
mCurrentPoint = Cursor.Position;
Show(); // to add here the bool of the Mouse Cursor to hide or not to hide
}
}
void HandleTimer(object sender, EventArgs e)
{
float dx = speed * (mTargetPoint.X - mCurrentPoint.X);
float dy = speed * (mTargetPoint.Y - mCurrentPoint.Y);
if (mFirstTime)
{
mFirstTime = false;
mCurrentPoint.X = mTargetPoint.X;
mCurrentPoint.Y = mTargetPoint.Y;
Left = (int)mCurrentPoint.X - Width/2;
Top = (int)mCurrentPoint.Y - Height/2;
return;
}
mCurrentPoint.X += dx;
mCurrentPoint.Y += dy;
if (Math.Abs(dx) < 1 && Math.Abs(dy) < 1)
{
mTimer.Enabled = false;
}
else
{
// Update location
Left = (int)mCurrentPoint.X - Width/2;
Top = (int)mCurrentPoint.Y - Height/2;
mLastMagnifierPosition = new Point((int)mCurrentPoint.X, (int)mCurrentPoint.Y);
}
Refresh();
}
protected override void OnMouseDown(MouseEventArgs e)
{
mOffset = new Point(Width/2 - e.X, Height/2 - e.Y);
mCurrentPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
mTargetPoint = mCurrentPoint;
mTimer.Enabled = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (_doMove == true)
{
mTargetPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
mTimer.Enabled = true;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
/*if (mConfiguration.DoubleBuffered)
{
// Do not paint background (required for double buffering)!
}
else
{
base.OnPaintBackground(e);
}*/
base.OnPaintBackground(e);
}
protected override void OnPaint(PaintEventArgs e)
{
if (mBufferImage == null)
{
mBufferImage = new Bitmap(Width, Height);
}
Graphics bufferGrf = Graphics.FromImage(mBufferImage);
Graphics g;
/*if (mConfiguration.DoubleBuffered)
{
g = bufferGrf;
}
else
{*/
g = e.Graphics;
//}
if (mScreenImage != null)
{
Rectangle dest = new Rectangle(0, 0, Width, Height);
int w = (int)(Width/zoom);//mConfiguration.ZoomFactor);
int h = (int)(Height/zoom);//mConfiguration.ZoomFactor);
int x = Left - w/2 + Width/2;
int y = Top - h/2 + Height/2;
g.DrawImage(
mScreenImage,
dest,
x, y,
w, h,
GraphicsUnit.Pixel);
}
e.Graphics.DrawImage(mBufferImage, 0, 0, Width, Height);
}
//--- Data Members ---
#region Data Members
private bool hidecursor;
private float zoom;
private float hue;
private float speed;
private Timer mTimer;
private Image mBufferImage = null;
private Image mScreenImage = null;
private Point mStartPoint;
private PointF mTargetPoint;
private PointF mCurrentPoint;
private Point mOffset;
private bool mFirstTime = true;
private static Point mLastMagnifierPosition = Cursor.Position;
#endregion
// New code \\
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
Point pt = Control.MousePosition;
int eX = pt.X - this.Left;
int eY = pt.Y - this.Top;
mOffset = new Point(0, 0);
mCurrentPoint = PointToScreen(new Point(eX + mOffset.X, eY + mOffset.Y));
mTargetPoint = mCurrentPoint;
mTimer.Enabled = true;
this.Capture = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if (_doMove)
{
Left = (int)mCurrentPoint.X - Width/2;
Top = (int)mCurrentPoint.Y - Height/2;
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (_doMove == true)
{
_doMove = false;
}
else
{
_doMove = true;
}
}
}
}
這是放大鏡玻璃表代碼,這樣也許有人可以測試一下,看看在pictureBox上移動時有多模糊。
這是我如何打開/關閉Form1中的放大鏡玻璃。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.M))
{
if (mf == null)
{
mf = new Magnifier20070401.MagnifierForm();
mf.StartPosition = FormStartPosition.Manual;
mf.Location = Control.MousePosition;
mf.Show();
this.Select();
}
else if (mf.IsDisposed)
{
mf = new Magnifier20070401.MagnifierForm();
mf.StartPosition = FormStartPosition.Manual;
mf.Location = Control.MousePosition;
mf.Show();
}
else
{
mf.Close();
mf = null;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
到底是什麼問題?看起來它應該對我做這項工作。 – VisualMelon
VisualMelon也許我沒有在主題中提出正確的問題。我想將圖像大小更改爲pictureBox大小的原因是即使在縮放模式或Stretchimage模式下,pictureBox中的圖像也很模糊。我現在已經更新了我的問題,並附有圖像顯示的鏈接,當我使用放大鏡玻璃移動pictureBox時發生了什麼。如果我在窗體區域或屏幕區域上移動它,放大鏡會將一切變得平滑。但只有當我移動到pictureBox區域時,它纔是模糊的。 –
感謝您的澄清 – VisualMelon