Create a Bitmap image to show an hour worked using c# - c#

I am trying to make a Bitmap image to represent a block of one hour and show red as worked time and white as non-worked showing in periods of 10 minutes intervals. I am trying to get the result to look like below:
Any help or guidance would be greater appreciated. In the code below Tuple<int,DateTime>> the int is time block example 0,1,2,3.....21,22,23,24 and DateTime will hold the time worked.
public void DrawPeriod(IGrouping<int, Tuple<int, DateTime>> worked)
{
var bitmap = new Bitmap(640, 480);
for (var x = 0; x < bitmap.Width; x++)
{
for (var y = 0; y < bitmap.Height; y++)
{
bitmap.SetPixel(x, y, Color.Red);
}
}
bitmap.Save("worked.bmp");
}

Sorry I can't give a sample with the data you needed. But you can do it something like this. This can be achieved by using System.Drawing.Graphics.
var sampleData = new int[] { 1, 2, 3, 13, 4, 5, 6, 12, 7, 8, 9 };
var bitmapHeight = 250;
var barWidth = 50;
var bitmap = new Bitmap(sampleData.Length * barWidth, bitmapHeight);
int currentX = 1;
foreach (var item in sampleData)
{
var result = item % 2;
Brush brush;
if (result == 0)
{
brush = Brushes.Red;
}
else
{
brush = Brushes.White;
}
using (Graphics graphics = Graphics.FromImage(bitmap))
{
var rectangle = new Rectangle(currentX, 0, barWidth, bitmapHeight);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.FillRectangle(brush, rectangle);
// Set Text
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
graphics.DrawString(item.ToString(), drawFont, drawBrush, currentX + 15, bitmapHeight / 2);
}
currentX = currentX + 50;
}
// Border
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawRectangle(new Pen(Brushes.Black, 5), new Rectangle(0, 0, bitmap.Width, bitmap.Height));
}
bitmap.Save(FileName);
Sample Output

Related

How to split a random image into 9 equal parts for puzzle game

I am trying to split an image into 9 equal parts. Like we see in a puzzle game. Image can be any random image. I am trying some code but it is not splitting it into equal parts - it needs coordinate values for rectangles, but I need simple general code to split an image into equal parts.
I've an image. I want to crop it or split into 9 equal parts. But the below code just crops the same part of the image from the right top corner every time.
var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var index = i * 3 + j;
imgarray[index] = new Bitmap(104, 104);
var graphics = Graphics.FromImage(imgarray[index]);
graphics.DrawImage(img, new Rectangle(0, 0, 104, 104), new Rectangle(i * 104, j * 104, 104, 104), GraphicsUnit.Pixel);
graphics.Dispose();
}
}
Some other tidy ups...
var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for (int i = 0; i < 9; i++)
{
imgarray[i] = new Bitmap(104, 104);
var graphics = Graphics.FromImage(imgarray[i]);
graphics.DrawImage (
img,
new Rectangle(0, 0, 104, 104),
new Rectangle( (i%3) * 104, (i/3) * 104, 104, 104),
GraphicsUnit.Pixel);
graphics.Dispose();
}
}

how to cut image in 9 pieces and control width and height?

HI guys i have problem in this code i want to display my photo cut in 9 pieces and display it in 9 pictureboxs to make puzzle game wish anyone could help .
Thanks In Advance
var knight = new Image[9];
var H = Image.FromFile("1425435_630471227004342_2061223205_o.jpg");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var index = i * 3 + j;
knight[index] = new Bitmap(200,200);
var m = Graphics.FromImage(knight[index]);
Rectangle r = new Rectangle( i * (knight[index].Width / 3),
j*(knight[index].Height / 3),
knight[index].Width / 3,
knight[index].Height / 3);
m.DrawImage(H, r, r, GraphicsUnit.Pixel);
m.Dispose();
}
}
pictureBox1.Image = knight[0];
pictureBox2.Image = knight[1];
pictureBox3.Image = knight[2];
pictureBox4.Image = knight[3];
pictureBox5.Image = knight[4];
pictureBox6.Image = knight[5];
pictureBox7.Image = knight[6];
pictureBox8.Image = knight[7];
pictureBox9.Image = knight[8];
Change this
Rectangle r = new Rectangle( i * (knight[index].Width / 3),
j*(knight[index].Height / 3),
knight[index].Width / 3,
knight[index].Height / 3);
m.DrawImage(H, r, r, GraphicsUnit.Pixel);
to this:
out of the loop:
// old size of the parts:
int ow = H.Width / 3;
int oh = H.Height / 3;
// new size of the parts:
int nw = knight[0].Width;
int nh = knight[0].Height;
// inner loop:
Rectangle rDest = new Rectangle(0, 0, nw, nh);
Rectangle rSource = new Rectangle(i * ow, j * oh, ow, oh);
m.DrawImage(H, rDest, rSource , GraphicsUnit.Pixel);
Note: If the proportions of the Original Image and the 9 PBs is different there will be a distortion:
You are not using a separate source rectangle (as TaW mentioned). Also you are not fully occupying the picturebox.
Try this code:
knight[index] = new Bitmap(640, 360); //enter the size of the original image
Rectangle src = new Rectangle(i * (knight[index].Width / 3), j * (knight[index].Height / 3), knight[index].Width / 3, knight[index].Height / 3);
Rectangle des = new Rectangle(0, 0, knight[index].Width, knight[index].Height);
m.DrawImage(H, des, src, GraphicsUnit.Pixel);

How to create circle with bitmap c#

Hello How to draw a circle with the bitmap. That is, I need to get the image of the circle, using it to draw something.
Use ColorTranslator.FromHtml for this purpose.
This will give you the corresponding System.Drawing.Color:
using (Bitmap btm = new Bitmap(25, 30))
{
using (Graphics grf = Graphics.FromImage(btm))
{
using (Brush brsh = new SolidBrush(ColorTranslator.FromHtml("#ff00ffff")))
{
grf.FillEllipse(brsh, 0, 0, 19, 19);
}
}
}
Or Refer Code:
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
Pen blackPen = new Pen(Color.Black);
int x = pictureBox1.Width/4;
int y = pictureBox1.Height/4;
int width = pictureBox1.Width / 2;
int height = pictureBox1.Height / 2;
int diameter = Math.Min(width, height);
g.DrawEllipse(blackPen, x, y, diameter, diameter);
pictureBox1.Image = bmp;
If the PictureBox already contains a bitmap, replace the first and second lines with:
Graphics g = Graphics.FromImage(pictureBox1.Image);
Referance Link:
http://www.c-sharpcorner.com/Forums/Thread/30986/
Hope Its Helpful.
Bitmap b = new Bitmap(261, 266);// height & width of picturebox
int xo = 50, yo = 50;// center of circle
double r, rr;
r = 20;
rr = Math.Pow(r, 2);
for (int i = xo - (int)r; i <= xo + r; i++)
for (int j = yo - (int)r; j <= yo + r; j++)
if (Math.Abs(Math.Pow(i - xo, 2) + Math.Pow(j - yo, 2) - rr) <= r)
b.SetPixel(i, j, Color.Black);
pictureBox1.Image = b;

Captcha image - ASP.NET

I'm in the process of making my own Captcha check on my website.
Everything's working, except I need some blurryness/effects on my text that's not viewable by a webcrawler etc.
Some of the code used to generate the text on the image:
Bitmap BitMap = new Bitmap(#"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
Graphics g = Graphics.FromImage(BitMap);
g.DrawString(""+RandomNumberString+"", new Font("Tahoma", 40), Brushes.Khaki, new PointF(1, 1));
pictureBox1.Image = BitMap;
Example:
What can I do to get my effects/blurryness on my text?
Thank you!
Why roll out your own captcha when reCAPTCHA is free, accessible (through the audio option, making it usable for people with visual issues) and at the same time helps digitize various publications? There's even a .NET implementation.
Edit:
Seeing how it's for fun, having a look at "An ASP.NET Framework for Human Interactive Proofs" might give you some good ideas. Especially the ImageHipChallenge as it includes image distortion code examples.
For example:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int newX = (int)(x + (distortion * Math.Sin(Math.PI * y / 64.0)));
int newY = (int)(y + (distortion * Math.Cos(Math.PI * x / 64.0)));
if (newX < 0 || newX >= width) newX = 0;
if (newY < 0 || newY >= height) newY = 0;
b.SetPixel(x, y, copy.GetPixel(newX, newY));
}
}
Which will move the pixels in a wave like fashion. Such as in the second word of your example.
Have a look at this tutorial. There you will find a code example on how to create a CAPTCHA using C# and the DrawString method.
Hope, this helps.
I've used this for about 5 years and it doesn't involve any integration with horrid 3rd party APIs.
http://www.codeproject.com/KB/aspnet/CaptchaImage.aspx
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)
{
LoadCaptcha();[![enter image description here][1]][1]
}
}
public void LoadCaptcha()
{
try
{
Bitmap objBitmap = new Bitmap(130, 60);
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.Clear(Color.White);
Random objRandom = new Random();
objGraphics.DrawLine(Pens.Black, objRandom.Next(0, 50), objRandom.Next(10, 30), objRandom.Next(0, 200), objRandom.Next(0, 50));
objGraphics.DrawRectangle(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(0, 20), objRandom.Next(50, 80), objRandom.Next(0, 20));
objGraphics.DrawLine(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(10, 50), objRandom.Next(100, 200), objRandom.Next(0, 80));
Brush objBrush =
default(Brush);
//create background style
HatchStyle[] aHatchStyles = new HatchStyle[]
{
HatchStyle.BackwardDiagonal, HatchStyle.Cross, HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal, HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,
HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross, HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid, HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,
HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard, HatchStyle.LargeConfetti, HatchStyle.LargeGrid, HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal
};
////create rectangular area
RectangleF oRectangleF = new RectangleF(0, 0, 300, 300);
objBrush = new HatchBrush(aHatchStyles[objRandom.Next(aHatchStyles.Length - 3)], Color.FromArgb((objRandom.Next(100, 255)), (objRandom.Next(100, 255)), (objRandom.Next(100, 255))), Color.White);
objGraphics.FillRectangle(objBrush, oRectangleF);
//Generate the image for captcha
string captchaText = string.Format("{0:X}", objRandom.Next(1000000, 9999999));
//add the captcha value in session
Session["CaptchaVerify"] = captchaText;
Font objFont = new Font("Courier New", 15, FontStyle.Bold);
//Draw the image for captcha
objGraphics.DrawString(captchaText, objFont, Brushes.Black, 20, 20);
// objBitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Gif);
byte[] _bytes;
using (MemoryStream ms = new MemoryStream())
{
objBitmap.Save(ms, ImageFormat.Bmp);
_bytes = ms.ToArray();
}
imgcaptcha.ImageUrl = "data:image;base64," + Convert.ToBase64String(_bytes);
ImageCapchaSubmit.ImageUrl = "data:image;base64," + Convert.ToBase64String(_bytes);
}
catch (Exception)
{
}
}

c# bitmap filling

I want to create a bitmap of size 160*160 and split it into four squares with each square filled with one color. How can this be done?
Just in case anyone needs a method solving this specific problem in a more general way, I wrote an extension method, taking colors and an integer that states how many tiles it should split off in x and y direction:
public static void FillImage(this Image img, int div, Color[] colors)
{
if (img == null) throw new ArgumentNullException();
if (div < 1) throw new ArgumentOutOfRangeException();
if (colors == null) throw new ArgumentNullException();
if (colors.Length < 1) throw new ArgumentException();
int xstep = img.Width / div;
int ystep = img.Height / div;
List<SolidBrush> brushes = new List<SolidBrush>();
foreach (Color color in colors)
brushes.Add(new SolidBrush(color));
using (Graphics g = Graphics.FromImage(img))
{
for (int x = 0; x < div; x++)
for (int y = 0; y < div; y++)
g.FillRectangle(brushes[(y * div + x) % colors.Length],
new Rectangle(x * xstep, y * ystep, xstep, ystep));
}
}
The four squares, the OP wanted would be produced with:
new Bitmap(160, 160).FillImage(2, new Color[]
{
Color.Red,
Color.Blue,
Color.Green,
Color.Yellow
});
You can try something like
using (Bitmap b = new Bitmap(160, 160))
using (Graphics g = Graphics.FromImage(b))
{
g.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 79, 79);
g.FillRectangle(new SolidBrush(Color.Red), 79, 0, 159, 79);
g.FillRectangle(new SolidBrush(Color.Green), 0, 79, 79, 159);
g.FillRectangle(new SolidBrush(Color.Yellow), 79, 79, 159, 159);
b.Save(#"c:\test.bmp");
}

Categories

Resources