PDA

View Full Version : سوال: ذخیره کردن بک گراند یک پیکچر باکس برای نمایش در اجراهای بعدی



adineh67
پنج شنبه 11 دی 1393, 18:06 عصر
سلام به همگی
توی برنامه یه پیکچرباکس دارم که کاربر میتونه به دلخواه خودش براش بک گراند تعیین کنه.تصویر انتخابی کاربر در یک فایل بیت مپ ذخیره میشه و توسط برنامه ویرایش شده و به عنوان بک گراند نمایش داده میشه.
حالا من میخوام آخرین تصویر تعیین شده توسط کاربر در اجرای دوباره ی برنامه نمایش داده بشه.ممنون میشم در این مورد راهنماییم کنید.

Share & Learn
جمعه 12 دی 1393, 09:49 صبح
سلام
تا حالا این کار رو انجام ندادم اما راهی که الان به ذهنم می رسه اینه که شما هر دفعه که تصویر تغییر می کنه اون رو تو دایرکتوری مورد نظرتون با یه اسم ثابت کپی کنید (با خاصیت اوررایت که تعداد عکس زیاد نشه)
و تو رویداد فرم لود برنامه هم تصویر پیکچرباکس رو از اون دایرکتوری مقدار دهی کنید، فکر می کنم این راه عملی باشه!
موفق باشید :)

Mahmoud Zaad
جمعه 12 دی 1393, 09:51 صبح
سلام
می تونید آخرین تصویر رو توی دایرکتوری برنامه و آدرسش رو توی setting برنامه ذخیره کنید. حالا موقع لود فرم چک کنید اگه چیزی توی setting بود یعنی عکسی در کار هست و نمایشش میدید اگر هم نبود که یا پیکچر باکس خالی می مونه یا یه تصویر پیش فرض نمایش داده میشه. البته ممکنه یه حالتی هم پیش بیاد که آدرس توی setting ذخیره شده ولی عکس در محل دایرکتوری نیست که این رو هم با یه if حل کنید.

adineh67
جمعه 12 دی 1393, 18:26 عصر
این کدو تو رویداد لود صفحه نوشتم



string savepath = AppDomain.CurrentDomain.BaseDirectory;
Bitmap bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
Graphics ModelGraphic;
if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Up" + ".png") == false
{
bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
else if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Up" + ".png") == true
{
if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Up" + ".png") == true
bitmap = new Bitmap(@savepath + "\\" + "Files" + "\\" + "Up" + ".png");
bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Front" + ".png") == false
{
bitmap1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
else if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Up" + ".png") == true
{
if System.IO.File.Exists(@savepath + "\\" + "Files" + "\\" + "Front" + ".png") == true
bitmap1 = new Bitmap(@savepath + "\\" + "Files" + "\\" + "Front" + ".png");
bitmap1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
ModelGraphic1 = Graphics.FromImage(bitmap1);
Pen Line = new Pen(Color.Black, 1);
ModelGraphic1.DrawLine(Line, 501, 0, 501, 256);
ModelGraphic1.DrawLine(Line, 501, 0, 498, 3);
ModelGraphic1.DrawLine(Line, 501, 0, 504, 3);
ModelGraphic1.DrawLine(Line, 1002, 126, 999, 123);
ModelGraphic1.DrawLine(Line, 1002, 126, 999, 129);
ModelGraphic1.DrawLine(Line, 0, 126, 1002, 126);
Image newImage = Image.FromFile(savepath + "\\" + "balloon-blue-C-SMALL_DOT.png");
Point ulCorner = new Point(x - 8, y - 16);
Point ulCorner1 = new Point(x - 8, z - 16);
ModelGraphic1.DrawImage(newImage, ulCorner1);
pictureBox1.Image = bitmap;
}



موقع اجرا با اینکه عکس Up در سیستم وجود داره ولی چیزیو نمایش نمیده با توجه به کدی که گذاشتم ممنون میشم راهنماییم کنید

Share & Learn
جمعه 12 دی 1393, 18:59 عصر
اول اینکه پیشنهاد می کنم به جای اینکه هی بنویسید System.IO از این name space بالای برنامه استفاده کنید:


using System.IO;


بعد هم اینکه اگر بجای چک کردن داخل if از try...catch استفاده کنید ok می شه

string Dir = AppDomain.CurrentDomain.BaseDirectory + "up.png";
private void Form1_Load(object sender, EventArgs e)
{
try
{
pictureBox1.Image = Image.FromFile(Dir);
}
catch
{
pictureBox1.Visible = false;
}
}

تو کد بالا مسیری که برای عکس بهش دادم داخل پوشه ی bin\Debug برنامه هست

تست کنید ببینید کارتون رو راه میندازه؟

adineh67
جمعه 12 دی 1393, 19:35 عصر
نه متاسفانه مشکلم حل نشد :افسرده::افسرده:

Share & Learn
جمعه 12 دی 1393, 19:41 عصر
نه متاسفانه مشکلم حل نشد :افسرده::افسرده:

من الان یه برنامه که درست کار می کنه براتون آماده می کنم

Share & Learn
جمعه 12 دی 1393, 23:55 عصر
ببخشید طول کشید، خدمت شما:

127184

adineh67
شنبه 13 دی 1393, 00:24 صبح
ببخشید طول کشید، خدمت شما:


سپاس بابت وقتی که گذاشتید،لطف کردید

adineh67
شنبه 13 دی 1393, 00:27 صبح
شرمنده ورژن ویژوال استادیوتون چنده؟
من با 2010 کار میکنم بازش نمیکنه

Share & Learn
شنبه 13 دی 1393, 00:49 صبح
مال من 2012 هست
فایل اجراییش که اجرا می شه نه؟
همونه که می خواین؟
اگه همونه فایل Form1.cs رو با notepad باز کنید و از کدهاش استفاده کنید

adineh67
شنبه 13 دی 1393, 00:55 صبح
چون ورژن برنامه ی من پایین هست اجراش نمیکنه ولی از فرمش استفاده میکنم
بازم ممنونم :لبخندساده:

adineh67
شنبه 13 دی 1393, 09:50 صبح
سلام به همگی
دوستان من مشکلمو بالا مطرح کردم و با توجه به کد دوست عزیز Share & Learn (http://barnamenevis.org/member.php?317640-Share-amp-Learn) یکمی اصلاحش کردم ولی باز همچنان تصویر ذخیره شده رو نمایش نمیده
البته تصویری که دارم یسری تغییرات روش انجام میشه و بعد از اون باید نمایش داده بشه نمونه تصویر و کد رو گذاشتم ممنون میشم راهنماییم کنید.

127191




try
{
string savepath = AppDomain.CurrentDomain.BaseDirectory;
Bitmap bitmap = new Bitmap(pictureBox1.Size.Width,pictureBox1.Size.Hei ght,System.Drawing.Imaging.PixelFormat.Format32bpp Argb);
Bitmap bitmap1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
Graphics ModelGraphic;
Graphics ModelGraphic1;
string stp1 = @savepath + "Files" + "\\" + "Up";
if ((File.Exists(stp1 + ".png") == false) &&
(File.Exists(stp1 + ".bmp") == false) &&
(File.Exists(stp1 + ".jpg") == false) &&
(File.Exists(stp1 + ".jpeg") == false))
{
bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
else if ((File.Exists(stp1 + ".png") == true) ||
(File.Exists(stp1 + ".bmp") == true) ||
(File.Exists(stp1 + ".jpg") == true) ||
(File.Exists(stp1 + ".jpeg") == true))
{
if (System.IO.File.Exists(stp1 + ".png") == true)
pictureBox1.Image = GetCopyImage(stp1 + ".png");
if (System.IO.File.Exists(stp1 + ".bmp") == true)
pictureBox1.Image = GetCopyImage(stp1 + ".bmp");
if (System.IO.File.Exists(stp1 + ".jpg") == true)
pictureBox1.Image = GetCopyImage(stp1 + ".jpg");
if (System.IO.File.Exists(stp1 + ".jpeg") == true)
pictureBox1.Image = GetCopyImage(stp1 + ".jpeg");
bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
}
string stp = @savepath + "Files" + "\\" + "Front";
if ((System.IO.File.Exists(stp + ".png") == false) &&
(System.IO.File.Exists(stp + ".bmp") == false) &&
(System.IO.File.Exists(stp + ".jpg") == false) &&
(System.IO.File.Exists(stp + ".jpeg") == false))
{
bitmap1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height,System.Drawing.Imaging.Pix elFormat.Format32bppArgb);
}
else if ((System.IO.File.Exists(stp + ".png") == true) ||
(System.IO.File.Exists(stp + ".bmp") == true) ||
(System.IO.File.Exists(stp + ".jpg") == true) ||
(System.IO.File.Exists(stp + ".jpeg") == true))
{
if (System.IO.File.Exists(stp + ".png") == true)
pictureBox2.Image = GetCopyImage(stp + ".png");
if (System.IO.File.Exists(stp + ".bmp") == true)
pictureBox2.Image = GetCopyImage(stp + ".bmp");
if (System.IO.File.Exists(stp + ".jpg") == true)
pictureBox2.Image = GetCopyImage(stp + ".jpg");
if (System.IO.File.Exists(stp + ".jpeg") == true)
pictureBox2.Image = GetCopyImage(stp + ".jpeg");
bitmap1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height,System.Drawing.Imaging.Pix elFormat.Format32bppArgb);
}
ModelGraphic = Graphics.FromImage(bitmap);
ModelGraphic1 = Graphics.FromImage(bitmap1);
Pen Line = new Pen(Color.Black, 1);
ModelGraphic1.DrawLine(Line, 501, 0, 501, 256);
ModelGraphic1.DrawLine(Line, 501, 0, 498, 3);
ModelGraphic1.DrawLine(Line, 501, 0, 504, 3);
ModelGraphic1.DrawLine(Line, 1002, 126, 999, 123);
ModelGraphic1.DrawLine(Line, 1002, 126, 999, 129);
ModelGraphic1.DrawLine(Line, 0, 126, 1002, 126);
ModelGraphic.DrawLine(Line, 501, 0, 501, 256);
ModelGraphic.DrawLine(Line, 501, 0, 498, 3);
ModelGraphic.DrawLine(Line, 501, 0, 504, 3);
ModelGraphic.DrawLine(Line, 1002, 126, 999, 123);
ModelGraphic.DrawLine(Line, 1002, 126, 999, 129);
ModelGraphic.DrawLine(Line, 0, 126, 1002, 126);
int x, y, z;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
x = Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value);
x = x / 2;
if (x < 0)
x = (501 - x) * (-1);
else
x = x + 501;
y = Convert.ToInt32(dataGridView1.Rows[i].Cells[14].Value);
y = y / 4;
if (y <= 0)
{
y = y * (-1);
y = (126 + y);
}
else
{
y = y - 126;
if (y < 0)
y = y * (-1);
}
z = Convert.ToInt32(dataGridView1.Rows[i].Cells[15].Value);
z = z / 4;
if (z <= 0)
{
z = z * (-1);
z = (126 + z);
}
else
{
z = z - 126;
if (z < 0)
z = z * (-1);
}
Image newImage = Image.FromFile(savepath + "\\" + "balloon-blue-C- SMALL_DOT.png");
Point ulCorner = new Point(x - 8, y - 16);
Point ulCorner1 = new Point(x - 8, z - 16);
ModelGraphic.DrawImage(newImage, ulCorner);
ModelGraphic1.DrawImage(newImage, ulCorner1);
pictureBox1.Image = bitmap;
pictureBox2.Image = bitmap1;
}

}
catch
{
}




private Image GetCopyImage(string path)
{
using (Image im = Image.FromFile(path))
{
Bitmap bm = new Bitmap(im);
return bm;
}
}

adineh67
دوشنبه 15 دی 1393, 19:35 عصر
دوستان کدی که گذاشتم ابهام داره؟

Mahmoud Zaad
دوشنبه 15 دی 1393, 20:12 عصر
با سلام
دلیلش اینه که پیکچرباکس های شما در ابتدا خالی هستند بعد در شرط زیر هم تصویری پیدا نمی کنه و در همین بلاک می مونه و تصویری نشون نمیده.
if ((File.Exists(stp1 + ".png") == false) && (File.Exists(stp1 + ".bmp") == false) &&
(File.Exists(stp1 + ".jpg") == false) &&
(File.Exists(stp1 + ".jpeg") == false))

adineh67
دوشنبه 15 دی 1393, 23:08 عصر
الان این قسمت کدمو اصلاح کردم وقتی تریس میکنم میبینم عکسو پیدا میکنه تخصیص هم میده ولی موقع اجرا نمایش نمیده :ناراحت:

aminmousavi
سه شنبه 16 دی 1393, 02:12 صبح
سلام دوست عزیز .
وقت نکردم کدی که نوشتید رو دقیق ببینم . اما یک تیکه کد به سورس قبلی اضافه کردم ببینین مشکلتون حل میشه :
http://greendeveloper.ir/files/StreachImage.rar (http://greendeveloper.ir/files//StreachImage.rar)

Share & Learn
سه شنبه 16 دی 1393, 03:40 صبح
الان این قسمت کدمو اصلاح کردم وقتی تریس میکنم میبینم عکسو پیدا میکنه تخصیص هم میده ولی موقع اجرا نمایش نمیده :ناراحت:

سلام
بعد از جایی که عکس رو اختصاص می ده این خط رو بنویسید
فکر کنم مشکل حل بشه


InitializeComponent();