How to save an image file using C#? - c#

I'm working with C#, só I start one "Windows Form application" in order to use the "picturebox", where I can draw some things. I would like to know if there's a way to save a file in some kind of file (like JPG). Here's an example, where I draw a simple line in a pictureBox with 300 of Height and 300 of Width.
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 AskStack
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Here I declare 'MyDraw' and a Pen that I'm gonna use
Graphics MyDraw;
Pen BluePen = new Pen(Color.Blue, 10);
// What happens when I click in my button
private void ButtonDraw_Click(object sender, EventArgs e)
{
MyDraw.Clear(Color.White);
// Here I relate MyDraw to the pictureBox that I have in my Form
MyDraw = pictureBox1.CreateGraphics();
// Here I Just draw a simple line
MyDraw.DrawLine(BluePen, 25, 25, 80, 80);
// I don't know exactly what this does
MyDraw.Save();
}
}
}
I would like to know if there's a way to save this image in a file where I can easily acess it. Also, I would like to know if there's a way to save an JPG file and dimension the file's size WITHOUT using a pictureBox. Thanks!!!

You can try with this line of code:
pictureBox1.Image.Save(#"Path",ImageFormat.Jpeg);

Related

Drawing a circle on a pictureBox from another winform

I have two winforms in my application. One of the forms has a picturebox with a jpg loaded of our building plan. The main form has code that does facial recognition identifying people coming into certain areas. I have been asked to modify this program to show an identified individual's location on the building plan. I have a database that has all the X,Y coordinates of the locations that should map to the building plan image. I have looked around and tried to find some code that will draw a circle on the map at the X,Y coordinates as the person progresses through areas of the building by erasing all the existing circles and updating this new one. So on the map form I put in the following code:
public void DrawCircle(int x, int y)
{
Graphics gf = pictureBox1.CreateGraphics();
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 400, 400));
pictureBox1.Refresh();
}
Then from the update method (right now a button click for testing) on the main form I call this method on the map form. The method gets called, but the circle doesn't show up on the form. I have tried both Refresh and Invalidate and neither method seems to draw the circle on the image.
I haven't done winforms development for years, so I'm sure I am missing some plumbing somewhere. Here is the code on the mainform:
LocationMap map = new LocationMap();
public Form1()
{
InitializeComponent();
//set up signalR
UserName = "MovementHub1";
ConnectAsync();
//show the map screen
map.Show();
map.WindowState = FormWindowState.Maximized;
...
Then in a click event (for testing right now) I have this code:
private void button2_Click(object sender, EventArgs e)
{
map.DrawCircle(340, 258);
}
Once I get the circle drawn on the other form, then I will remove the code from the click event and move it another event that does the updating on the location. If it's possible, I would like to put a label by the circle that has the person's name. Right now this is a proof of concept, I just need help getting the circle on the form to start with.
Thanks.
I tried It out by myself and came up with that:
Form1.cs
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 StackoverflowHelp
{
public partial class Form1 : Form
{
Form2 form = new Form2();
public Form1()
{
InitializeComponent();
form.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
form.DrawCircle(100, 100);
}
}
}
Form2.cs
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 StackoverflowHelp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
DrawCircle(10, 10);
}
public void DrawCircle(int x, int y)
{
Graphics gf = Graphics.FromImage(pictureBox1.Image);
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 20, 20));
gf.Dispose();
pictureBox1.Refresh();
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
Instead of calling CreateGraphics() on the picturebox I created the graphics object using the current image.

C# Moving a cross into a picturebox based on the Thumbstick Position

I want to draw a cross into a picturebox. In the next step I want to move the cross based on my Data.
Which is the Thumbsticks position. I want it to appear similar to the figure below.
Here is my Code:
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;
using SlimDX;
using SlimDX.XInput;
namespace Controller_Test
{
public partial class Form1 : Form
{
GamepadState Controller = new GamepadState(UserIndex.One);
Graphics drawArea;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
drawArea = pictureBox1.CreateGraphics();
}
private void timer1_Tick(object sender, EventArgs e)
{
Controller.Update();
var LeftStick = Controller.LeftStick;
float LeftStickX = LeftStick.Position.X;
float LeftStickY = LeftStick.Position.Y;
Pen blackPen = new Pen(Color.Black);
// horizontal Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
// vertical Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
}
}
public class GamepadState
{Get Postiton......}
}
My problem now is, if I move the Thumbstick every time a new cross is drawn but the old one is still in the
Picturebox like in the figure below. If i use the pictureBox1.Invalidate():/pictureBox1.Refresh(); Funtcion
the cross becomes invisible and is not displayed anymore. The interval of the timer is on 1.
What could i change in order to get the code to work?

How to Change the color of each pixel in a image by using Magick.Net

I'm new to the Magick.Net. I tried to change the color of each pixel in the image but there's no change in the new image. Here's my code. Could anyone tell me what's going on? Thanks a lot.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
namespace MagickTutor
{
class Program
{
static void Main(string[] args)
{
MagickImage image = new MagickImage();
image.Read("C:\\.....\\test1.png");
foreach (Pixel p in image.GetWritablePixels()) {
p.SetChannel(0, 65535);
}
image.Write("C:\\.....\\test2.png");
}
}
}
You should call the Write method of the WritablePixelCollection class that is returned by image.GetWritablePixels() to make sure the pixels are written to the image.
You could also do what you are doing in a different way:
using (MagickImage image = new MagickImage())
{
image.Read(#"C:\.....\test1.png");
image.Evaluate(Channels.Red, EvaluateOperator.Set, Quantum.Max);
image.Write(#"C:\.....\test2.png");
}

Cant Get C# Form To Change Red

So I am following the book "C# Game Programming - For Serious Game Creation" and I'm at a part in the book that has you create OpenGL form with Tao and change the color from black to red.
Here is my code which is exactly the same as in the book
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;
using Tao.OpenGl;
namespace GameLoop
{
public partial class Form1 : Form
{
FastLoop _fastLoop;
bool _fullscreen = false;
public Form1()
{
_fastLoop = new FastLoop(GameLoop);
InitializeComponent();
_openGLControl.InitializeContexts();
if (_fullscreen)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
}
void GameLoop(double elapsedTime)
{
Gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glFinish();
_openGLControl.Refresh();
}
}
}
I am not getting any errors at all, it just simply stays black. What are the possible reasons for why this could be happening?
code for other game loop
static void GameLoop(double elapsedTime)
{
// GameCode goes here
// GetInput
// Process
// Render
System.Console.WriteLine("Loop");
}
Thanks to Ali Kazmi and Matthew I figured it out. by puting the code into public Form1() It worked and I was able to conclude that the method was not updating and Im very new to C# I think it was because the other game loop was not letting it get that far. I removed it and it all works fine.

How to to shrink(scale) an entire graphics structure?

I’m trying to fit a lot of rectangles into a Bitmap, which will be displayed in a picturebox. In my real code, I figure out the total width and height of a rectangle that can encompass all them, and then I divide that by the size of the Bitmap, to get my scaling factor. The problem is I can’t figure out how to perform the scaling. The code below is a simple version of what I need to do.
Please keep in mind that I cannot rely on the picturebox’s scaling abilities (stretch), and I don’t want to simply apply the scale to the width and height of all the rectangles, because in my real code it wouldn’t work very well. I need a way to shrink it down in Graphics. It is important the Bitmap stays the same size that it is (300 X 300). Thanks. The below code is what I've gotten so far, but nothing changes with the size.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap BM = new System.Drawing.Bitmap(300, 300);
Pen PenTest = new System.Drawing.Pen(Brushes.Red);
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics GR = Graphics.FromImage(BM))
{
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
// I need a scale of 0.60 in this example, because 300/500 = .6
GR.ScaleTransform(.6F, .6F);//Doesn't Work. No Change at all in the size of the rectangle.
}
pictureBox1.Image = BM;
}
}
}
Graphics.ScaleTransform performs the transformation but it does not draw anything.
You would need to then draw a rectangle after performing the transform on the graphics object:
using (Graphics GR = Graphics.FromImage(BM))
{
// ....
GR.ScaleTransform(.6F, .6F);
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
}

Categories

Resources