Answer the question
In order to leave comments, you need to log in
How to loop through an image?
Good afternoon, there is a lab for creating a simple graphic editor, in which there is a task with which there are difficulties, you need to create a function that adds 1000 points to the image with coordinates set randomly, the color is also set randomly. I found a ready-made function, but there were problems with embedding it in a button (button3), constant errors "contains no definition". Please help.
Here is the function code:
public Color RandColor(Random rand)
{
return Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256));
}
public float NextF(Random rand, float min, float max)
{
return min + (float)rand.NextDouble() * (max - min);
}
public void Draw1000(Bitmap bmp, float r = 1F)
{
Random rand = new Random();
using (var g = Graphics.FromImage(bmp))
{
for (int i = 0; i < 1000; i++)
{
using (var br = new SolidBrush(rand.RandColor()))
{
g.FillEllipse(br, rand.NextF(0, bmp.Width) - r, rand.NextF(0, bmp.Height) - r, 2 * r, 2 * r);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp21
{
public partial class Form1 : Form
{
private Point PreviousPoint, point;
private Bitmap bmp;
private Pen blackPen;
private Graphics g;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
blackPen = new Pen(Color.Black, 4);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PreviousPoint.X = e.X;
PreviousPoint.Y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{ point.X = e.X;
point.Y = e.Y;
g.DrawLine(blackPen, PreviousPoint, point);
PreviousPoint.X = point.X;
PreviousPoint.Y = point.Y;
pictureBox1.Invalidate();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image files (*.BMP, *.JPG, " + "*.GIF, *.PNG)|*.bmp;*.jpg;*.gif;*.png";
if (dialog.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(dialog.FileName);
int width = image.Width;
int height = image.Height;
pictureBox1.Width = width;
pictureBox1.Height = height;
bmp = new Bitmap(image, width, height);
pictureBox1.Image = bmp;
g = Graphics.FromImage(pictureBox1.Image);
}
}
public Color RandColor(Random rand)
{
return Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256));
}
public float NextF(Random rand, float min, float max)
{
return min + (float)rand.NextDouble() * (max - min);
}
public void Draw1000(Bitmap bmp, float r = 1F)
{
Random rand = new Random();
using (var g = Graphics.FromImage(bmp))
{
for (int i = 0; i < 1000; i++)
{
using (var br = new SolidBrush(rand.RandColor()))
{
g.FillEllipse(br, rand.NextF(0, bmp.Width) - r, rand.NextF(0, bmp.Height) - r, 2 * r, 2 * r);
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Title = "Сохранить картинку как ...";
savedialog.OverwritePrompt = true;
savedialog.CheckPathExists = true;
savedialog.Filter =
"Bitmap File(*.bmp)|*.bmp|" + "GIF File(*.gif)|*.gif|" + "JPEG File(*.jpg)|*.jpg|" + "PNG File(*.png)|*.png";
if (savedialog.ShowDialog() == DialogResult.OK)
{
string fileName = savedialog.FileName;
string strFilExtn = fileName.Remove(0,
fileName.Length - 3);
switch (strFilExtn)
{
case "bmp":
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpg":
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "tif":
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
case "png":
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
break;
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question