台灣最大程式設計社群網站
線上人數
1650
 
會員總數:243455
討論主題:188176
歡迎您免費加入會員
討論區列表 >> C# >> C# 在picturebox上做畫,如何改變大小且像素不變
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
C# 在picturebox上做畫,如何改變大小且像素不變
價值 : 100 QP  點閱數:83 回應數:2

樓主

劉品杰
門外漢
0 1
10 0
發送站內信

各位大神們好! 小弟我最近寫一個程式,是在picturebox上繪圖,因要繪圖的大小是128*64,由於這樣太小不好繪圖,所以寫了讓picturebox放大,可是當放大後,卻還是只能在原本的128*64裡繪圖,如果把新大小的picturebox重新定義為畫布,則儲存的大小就不是128*64,請問各位有什麼辦法呢? 抱歉第一次發文,有點冗長,請各位見諒! 謝謝大神!

https://imageshack.com/i/plve8otfp
此網址為我的介面

附上程式碼:

public partial class Form1 : Form{

Bitmap myImage;
int PBwidth;
int PBheight;
bool beginPaint = false;
bool beginMove = false;
int currentXpos;
int currentYpos;
Size bm_size;
int zoom;
int limit;

public Form1()
{
InitializeComponent();

}
private void Form1_Load(object sender, EventArgs e)
{
PBwidth = pictureBox1.Width;
PBheight = pictureBox1.Height;

myImage = new Bitmap(PBwidth, PBheight);
Graphics g = Graphics.FromImage(myImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.Clear(Color.White);
pictureBox1.Image = myImage;


bm_size = myImage.Size;
zoom = 1;
limit = 128;
}

private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "開始畫畫")
{
beginPaint = true;
button1.Text = "暫停畫畫";

}

else if (button1.Text == "暫停畫畫")
{
beginPaint = false;
button1.Text = "開始畫畫";
}
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (beginPaint == true)
{
if (e.Button == MouseButtons.Left)
{
beginMove = true;
currentXpos = e.X;
currentYpos = e.Y;
}
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
Graphics gp = Graphics.FromImage(myImage);
Pen myPen = new Pen(Color.Black, 2 );
myPen.StartCap = myPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

gp.DrawLine(myPen, currentXpos, currentYpos, e.X, e.Y);
pictureBox1.Image = myImage;
gp.Dispose();
currentXpos = e.X;
currentYpos = e.Y;
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left)
{
beginMove = false;
currentXpos = 0;
currentYpos = 0;
}
}

private void button2_Click(object sender, EventArgs e)
{
Graphics g = Graphics.FromImage(myImage);
g.Clear(Color.White);
pictureBox1.Image = myImage;
}

private void button3_Click(object sender, EventArgs e)
{
if (beginPaint == true)
{
MessageBox.Show("請先暫停畫畫");

}

else

{

SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
myImage.Save(sfd.FileName + @".bmp", ImageFormat.Bmp);
MessageBox.Show("儲存成功!");
}
return;
}
}

private void button4_Click(object sender, EventArgs e)
{

if (zoom < limit)
{
zoom = zoom * 2;
}
else if (zoom > 3)
{
MessageBox.Show("無法再放大了!");
}
pictureBox1.Size = new Size(zoom * bm_size.Width, zoom * bm_size.Height);
}

private void button5_Click(object sender, EventArgs e)
{
if (zoom > 1)
{
zoom = zoom / 2;
}
else
{
MessageBox.Show("無法再縮小了!");
}
pictureBox1.Size = new Size(zoom * bm_size.Width, zoom * bm_size.Height);
}
}

搜尋相關Tags的文章: [ 在picturebox上做畫 ] , [ 改變大小且像素不變 ] ,
本篇文章發表於2018-11-09 17:57
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
回應

小魚
檢舉此回應
自己寫Bitmap放大的函式,
再把放大過的Bitmap指定給PictureBox.
本篇文章回覆於2018-11-10 13:13
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
2樓
回應

小魚
檢舉此回應
這一篇 看是不是你要的?
本篇文章回覆於2018-11-10 13:19
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.