[ aleksandar_milojevic @ 02.02.2013. 00:01 ] @
Treba mi pomoc.
Radi se o sledecem: Ucitao sam sliku u PictureBox, izmenio sliku tako sto sam crtao nesto po njoj. Imam problem kako da sacuvam sliku sa izmenama koristeci SaveFileDialog.
Hvala unapred
[ Cofz922 @ 03.02.2013. 17:19 ] @
Probaj ovo :

button1_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
if(sf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(sf.FileName);
}
}
[ aleksandar_milojevic @ 03.02.2013. 18:21 ] @
Nece da radi.

Ove je kod koji sam ja uradio, ali nece u pictureboxu2 da mi sacuva izmenjenu sliku.
Ako ima neko neko ideju svakako bi mi dobro dosla...

Kod:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


namespace PrakticniRad
{
public partial class Form1 : Form
{
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
Rectangle rectCropArea;
//Image srcImage = null;



public Form1()
{
InitializeComponent();
bHaveMouse = false;
g = TargetPicBox.CreateGraphics();
}
bool canPaint = false;
Graphics g;

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void BtnCrop_Click(object sender, EventArgs e)
{
TargetPicBox.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
Graphics g = TargetPicBox.CreateGraphics();



//Draw the image on the Graphics object with the new dimesions
g.DrawImage(sourceBitmap, new Rectangle(0, 0, TargetPicBox.Width, TargetPicBox.Height),
rectCropArea, GraphicsUnit.Pixel);

//Good practice to dispose the System.Drawing objects when not in use.
sourceBitmap.Dispose();
}

private void SrcPicBox_MouseDown(object sender, MouseEventArgs e)
{
// Make a note that we "have the mouse".
bHaveMouse = true;

// Store the "starting point" for this rubber-band rectangle.
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;

// Special value lets us know that no previous
// rectangle needs to be erased.

// Display coordinates
lbCordinates.Text = "Coordinates : " + e.X.ToString() + ", " + e.Y.ToString();

ptLast.X = -1;
ptLast.Y = -1;

rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());
}

private void SrcPicBox_MouseUp(object sender, MouseEventArgs e)
{
// Set internal flag to know we no longer "have the mouse".
bHaveMouse = false;

// If we have drawn previously, draw again in that spot
// to remove the lines.
if (ptLast.X != -1)
{
Point ptCurrent = new Point(e.X, e.Y);

// Display coordinates
lbCordinates.Text = "Coordinates : " + ptOriginal.X.ToString() + ", " +
ptOriginal.Y.ToString() + " And " + e.X.ToString() + ", " + e.Y.ToString();

}

// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}

private void SrcPicBox_MouseMove(object sender, MouseEventArgs e)
{
Point ptCurrent = new Point(e.X, e.Y);

// If we "have the mouse", then we draw our lines.
if (bHaveMouse)
{
// If we have drawn previously, draw again in
// that spot to remove the lines.
if (ptLast.X != -1)
{
// Display Coordinates
lbCordinates.Text = "Coordinates : " + ptOriginal.X.ToString() + ", " +
ptOriginal.Y.ToString() + " And " + e.X.ToString() + ", " + e.Y.ToString();
}

// Update last point.
ptLast = ptCurrent;

// Draw new lines.

// e.X - rectCropArea.X;
// normal
if (e.X > ptOriginal.X && e.Y > ptOriginal.Y)
{
rectCropArea.Width = e.X - ptOriginal.X;

// e.Y - rectCropArea.Height;
rectCropArea.Height = e.Y - ptOriginal.Y;
}
else if (e.X < ptOriginal.X && e.Y > ptOriginal.Y)
{
rectCropArea.Width = ptOriginal.X - e.X;
rectCropArea.Height = e.Y - ptOriginal.Y;
rectCropArea.X = e.X;
rectCropArea.Y = ptOriginal.Y;
}
else if (e.X > ptOriginal.X && e.Y < ptOriginal.Y)
{
rectCropArea.Width = e.X - ptOriginal.X;
rectCropArea.Height = ptOriginal.Y - e.Y;

rectCropArea.X = ptOriginal.X;
rectCropArea.Y = e.Y;
}
else
{
rectCropArea.Width = ptOriginal.X - e.X;

// e.Y - rectCropArea.Height;
rectCropArea.Height = ptOriginal.Y - e.Y;
rectCropArea.X = e.X;
rectCropArea.Y = e.Y;
}
SrcPicBox.Refresh();
}
}

private void SrcPicBox_Paint(object sender, PaintEventArgs e)
{
Pen drawLine = new Pen(Color.Green);
drawLine.DashStyle = DashStyle.Dash;
e.Graphics.DrawRectangle(drawLine, rectCropArea);
}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SrcPicBox.ImageLocation = ofd.FileName;
}
}

private void TargetPicBox_MouseDown(object sender, MouseEventArgs e)
{
canPaint = true;
}

private void TargetPicBox_MouseUp(object sender, MouseEventArgs e)
{
canPaint = false;
prevX = null;
prevY = null;
}
int? prevX = null;
int? prevY = null;
private void TargetPicBox_MouseMove(object sender, MouseEventArgs e)
{
if (canPaint)
{
Pen pen = new Pen(Color.Black, float.Parse(textBox1.Text));
g.DrawLine(pen, new Point(prevX ?? e.X, prevY ?? e.Y), new Point(e.X, e.Y));
prevX = e.X;
prevY = e.Y;
}
}

private void button3_Click(object sender, EventArgs e)
{
{
SaveFileDialog sfdlg = new SaveFileDialog();
sfdlg.Title = "Save Dialog";
sfdlg.Filter = "Bitmap Images (*.bmp)|*.bmp|All files(*.*)|*.*";
if (sfdlg.ShowDialog(this) == DialogResult.OK)
{
Bitmap bmp = new Bitmap(TargetPicBox.BackgroundImage);
var g = Graphics.FromImage(bmp);
TargetPicBox.BackgroundImage.Save(sfdlg.FileName);
}
}
}
}
}