Can I use Bitmap in a Brush, in Visual C#? - c#

I want to use a picture instead of color in this code:
brush = new SolidBrush(Color.Red);
Ex: brush = new SolidBrush(PICTURE);
What should I do?

Maybe you can use one of the Fill methods in Graphics class and a TextureBrush?
Image image = Image.FromFile("bitmap file path");
TextureBrush textureBrush = new TextureBrush(image);
graphics.FillEllipse(myTextureBrush, 0, 0, 200, 200);

You could use the TextureBrush to do so
private void Button2_Click(System.Object sender, System.EventArgs e)
{
try
{
Bitmap image1 = (Bitmap) Image.FromFile(#"C:\Documents and Settings\" +
#"All Users\Documents\My Music\music.bmp", true);
TextureBrush texture = new TextureBrush(image1);
texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(texture,
new RectangleF(90.0F, 110.0F, 100, 100));
formGraphics.Dispose();
}
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("There was an error opening the bitmap." +
"Please check the path.");
}
}
Code snippet taken from MSDN.

Use ImageBrush.

Related

Getting Printing issue in C# windows forms

I'm trying to print windows forms and it is printing well but the issue is I want to print the form to the whole page. As you guys can see the the end of the page is empty. Is there a way to stretch is or something as solution. I'm new to C# and windows forms and don't know much, kindly anyone help !!. Explanation which is easy to understand.
here is my code:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void Print_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

Using the correct way to draw on a picturebox messes up the output

Im using a picturebox as a preview. When I was doing it incorrectly, the picturebox matched the printed output. When I changed it to use a bitmap, it looks poorly. I checked the DpiX and DpiY of the graphics object and it is 96 for all of them. The issue can be demonstrated with the following code. Create a form with a button and picturebox. Click the button. Then uncomment the line "//Correctway = true;" and observe the difference.
public partial class Form1 : Form
{
private bool Correctway;
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(Object sender, EventArgs e)
{
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
Bitmap Output;
//Correctway = true;
OutputText = "CERTIFICATION";
PrintFont = new Font("Arial", 16, FontStyle.Regular);
Output = new Bitmap(850, 1100);
if (Correctway)
g = Graphics.FromImage(Output);
else
g = pictureBox1.CreateGraphics();
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
if (Correctway)
pictureBox1.Image = Output;
}
}
"Poor" output. I wish I could describe it better, but I can't figure out what is happening.
This is the desired output, the output that is the result of printing and the screen output when I was using the incorrect procedure of creating a graphics object from the picturebox.
Note: I tried "You can also press CTRL+G to insert an image." but it does not work. With or without the image on the clipboard.
Taw found the answer in his comments. I am publishing it here to complete this post. The solution was to use g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
public partial class Form1 : Form
{
private bool Correctway;
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(Object sender, EventArgs e)
{
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
Bitmap Output;
//Correctway = true;
OutputText = "CERTIFICATION";
PrintFont = new Font("Arial", 16, FontStyle.Regular);
Output = new Bitmap(850, 1100);
if (Correctway)
g = Graphics.FromImage(Output);
else
g = pictureBox1.CreateGraphics();
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
if (Correctway)
pictureBox1.Image = Output;
}
}

Crop and load tiled image in picturebox c#

I have a tiled image like https://i.imgsafe.org/67397f9.png and want to load its parts as image for a picture box in some mouse events. actually I want simulate button behavior.
Bitmap source = new Bitmap(Properties.Resources.btn_close);
public Bitmap CropImage(Bitmap srcbmp, Rectangle dstcrp)
{
Bitmap bmp = new Bitmap(dstcrp.Width, dstcrp.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.DrawImage(srcbmp, 0, 0, dstcrp, GraphicsUnit.Pixel);
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 62), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
this is my codes that crop sections of image and load as bitmap for picture box.
I think it is not very professional and may have some memory usage problems, etc...
is there any simple way to do this?
2 solutions which i can think of
When you first load the form you can declare 4 image/bitmap variables 1 for each Mouse State Mouse Down, Leave Up and Enter so instead of recrating the images again and again you can just change the image when time is right.
var cropCoordinates= new Rectangle(new Point(0, 0), new Size(51, 30));
var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);
You can have 4 different image boxes for each state (layered 1 on top of the other )and show or hide when the time is needed

Load an image from media library onto canvas with WriteableBitmap image?

I am trying to load a picture selected from media library of windows phone and I have done upto choosing the required picture and I am unable to load image onto my canvas named area with this code :
void photochoosertask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
WriteableBitmap bitMap = new WriteableBitmap(200,200);
Extensions.LoadJpeg(bitMap, e.ChosenPhoto);
Canvas.SetLeft(area, 10);
Canvas.SetTop(area, 10);
bitMap.Render(area, null);
bitMap.Invalidate();
}
}
But I am unable to do with this code..any suggestions..??
or how to do this task ? Is this the correct way ?
Thanks
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(e.ChosenPhoto);
WriteableBitmap b = new WriteableBitmap(bi);
Image img = new Image();
img.Source = b;
Canvas.SetLeft(img, 10);
Canvas.SetTop(img, 10);
area.Children.Add(img);
}
In order to show the bitmap in your Canvas you would have to add an Image control to its Children collection, which uses the bitmap as its Source:
var bitmap = new WriteableBitmap(200, 200);
Extensions.LoadJpeg(bitmap, e.ChosenPhoto);
var image = new Image();
image.Source = bitmap;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);
As e.ChosenPhoto is a stream, you may perhaps also use a BitmapImage instead of a WriteableBitmap, and set its source stream to e.ChosenPhoto. You may then set the size of the Image control to the desired values.
var bitmap = new BitmapImage();
bitmap.SetSource(e.ChosePhoto);
var image = new Image();
image.Source = bitmap;
image.Width = 200;
image.Height = 200;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);

How to save and load in panel?

I want to save image from panel to bitmap and then I want to load the saved image after my Form comes out of minimizing mode.
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, panel1.Bounds);
bmp.Save(#"C:\Test");
panel1.BackgroundImage = Image.FromFile(#"C:\Test");
And what event should I use for minimizing event?
P.S. I am a C# beginner.
EDITED
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBitmap.jpeg", ImageFormat.Jpeg);
}
}

Categories