Diskuze: Uložení obrázku C#
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Člen
Zobrazeno 10 zpráv z 10.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Zkus tohle:
public void ExportToBmp(string path)
{
using(var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
{
pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
ImageFormat imageFormat = null;
var extension = Path.GetExtension(path);
switch (extension)
{
case ".bmp":
imageFormat = ImageFormat.Bmp;
break;
case ".png":
imageFormat = ImageFormat.Png;
break;
case ".jpeg":
case ".jpg":
imageFormat = ImageFormat.Jpeg;
break;
case ".gif":
imageFormat = ImageFormat.Gif;
break;
default:
throw new NotSupportedException("File extension is not supported");
}
bitmap.Save(path, imageFormat);
}
}
nebo lehčí varianta:
pictureBox1.Image.Save(@"C:\Users\HP\Pictures\test.jpg",ImageFormat.Jpeg);
Ani jedno nefunguje dávám sem celý kód:
Graphics g;
Point ep = new Point(0, 0);
Point sp = new Point(0, 0);
Pen myPen = new Pen(Color.Black, 5);
bool k = false;
TextBox txt = new TextBox();
Image File;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
{
k = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
k = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (k == true)
{
ep = e.Location;
g = Image.CreateGraphics();
g.DrawLine(myPen, sp, ep);
}
sp = ep;
}
private void pictureBox9_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox9.BackColor;
pictureBox1.BackColor = pictureBox9.BackColor;
}
private void pictureBox10_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox10.BackColor;
pictureBox1.BackColor = pictureBox10.BackColor;
}
private void pictureBox6_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox6.BackColor;
pictureBox1.BackColor = pictureBox6.BackColor;
}
private void pictureBox7_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox7.BackColor;
pictureBox1.BackColor = pictureBox7.BackColor;
}
private void pictureBox8_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox8.BackColor;
pictureBox1.BackColor = pictureBox8.BackColor;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox5.BackColor;
pictureBox1.BackColor = pictureBox5.BackColor;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox4.BackColor;
pictureBox1.BackColor = pictureBox4.BackColor;
}
private void pictureBox3_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox3.BackColor;
pictureBox1.BackColor = pictureBox3.BackColor;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox2.BackColor;
pictureBox1.BackColor = pictureBox2.BackColor;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
myPen.Width = float.Parse(numericUpDown1.Value.ToString());
}
private void pictureBox13_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox13.BackColor;
pictureBox1.BackColor = pictureBox13.BackColor;
}
private void pictureBox11_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox11.BackColor;
pictureBox1.BackColor = pictureBox11.BackColor;
}
private void button1_Click(object sender, EventArgs e)
{
g.Clear(Color.White);
}
private void pictureBox16_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox16.BackColor;
pictureBox1.BackColor = pictureBox16.BackColor;
}
private void pictureBox15_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox15.BackColor;
pictureBox1.BackColor = pictureBox15.BackColor;
}
private void pictureBox14_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox14.BackColor;
pictureBox1.BackColor = pictureBox14.BackColor;
}
private void pictureBox12_Click(object sender, EventArgs e)
{
myPen.Color = pictureBox12.BackColor;
pictureBox1.BackColor = pictureBox12.BackColor;
}
private void button2_Click(object sender, EventArgs e)
{
ExportToBmp("soubor");
}
public void ExportToBmp(string path)
{
using (var bitmap = new Bitmap(Image.Width, Image.Height))
{
Image.DrawToBitmap(bitmap, Image.ClientRectangle);
ImageFormat imageFormat = null;
var extension = Path.GetExtension(path);
switch (extension)
{
case ".bmp":
imageFormat = ImageFormat.Bmp;
break;
case ".png":
imageFormat = ImageFormat.Png;
break;
case ".jpeg":
case ".jpg":
imageFormat = ImageFormat.Jpeg;
break;
case ".gif":
imageFormat = ImageFormat.Gif;
break;
default:
throw new NotSupportedException("File extension is not supported");
}
bitmap.Save(path, imageFormat);
}
}
Jde to, ale uloží to jenom bílou plochu
Problém asi bude v tom, že kreslýš na Graphics, pictureBox1.Image vrací
obrázek pozadí, na kterém nic není.
Lepší způsob bude kreslit na obrázek a ten pak vykreslovat na picturebox
...
Udělal bych to nějak takto:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics gr = Graphics.FromImage(bmp);
// kreslení na obrázek ...
e.Graphics.DrawImage(bmp, 0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void Save(string path)
{
bmp.Save(path);
}
Zkus ještě tohle to by mělo už fungovat:
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
private void button2_Click(object sender, EventArgs e)
{
System.Drawing.Image currentImage = null;
var bin = (byte[]) (ImageToByte(AlbumArt.Image));
currentImage = Image.FromStream(new MemoryStream(bin));
using (Image image1 = Image.FromStream(new MemoryStream(bin)))
{
image1.Save("output.jpg", ImageFormat.Jpeg);
}
}
Funguje to ... jen se to kreslí na černé pozadí ... přidej:
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
gr.Clear(pictureBox1.BackColor);
}
Zobrazeno 10 zpráv z 10.