I want to plot points over an image that are a bit transparent. As in I'm able to see over what area are they present. Is there any way on C# .net platform to do so.??
Thanks.
This is one way to do it.
Image bitmap = new Bitmap(100, 100); // sample image, load your real image from file here
using (var g = Graphics.FromImage(bitmap))
{
g.FillRectangle(Brushes.Red, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); // Just to fill the background on the sample image, remove this
var transparentColor = Color.FromArgb(127, Color.Blue); // Create a semitransparent color
using(Brush brush = new SolidBrush(transparentColor))
{
// Create the dot
g.FillEllipse(brush, new Rectangle(10, 10, 25, 25));
// Create another dot
g.FillEllipse(brush, new Rectangle(25, 15, 25, 25));
}
}
myPictureBox.Image = bitmap; // display the image in an Imagebox (optional, you might use your image somewhere else)
Related
I want to combine two image but I cant success . Help me please.
When try the combine PictureBox show only first image but there is not second image , when I remove first image I can see second image.
Also I tried setting first image and draw the text on image that's also not working. Please help.
Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50);
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50));
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height);
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20);
Graphics Cizgi2 = Graphics.FromImage(bmp2);
Graphics Cizgi = Graphics.FromImage(bmp);
Cizgi.DrawImage(myimg, 0, 0);
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10);
Graphics g = Graphics.FromImage(image);
g.DrawImage(bmp, new Point(10, 0));
g.DrawImage(bmp2, new Point(0, bmp.Height + 10));
I want to image seems like first but i cant make
It looks like you are trying to concatenate two images vertically? It's pretty simple actually, you can look here (C# image concatenation), but I've also modified it for your needs. I think this should work:
float drawBorderX = 5;
float drawBorderY = 5;
//Set up our two images
Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap text = new Bitmap(barCode.Width, 50);
Graphics textGraphics = Graphics.FromImage(text);
//Draw the text to the bottom image.
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height));
textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY);
//Vertically concatenate the two images.
Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(barCode, 0, 0);
g.DrawImage(text, 0, barCode.Height);
Edit: Note that resultImage will contain the image you want, so you can set your PictureBox to be that image at the end.
My code is divided in three parts: PART 1) Drawing in a bitmap, PART 2) Saving the bitmap as a jpg image, PART 3) Reading the jpg file and find contours using Emgu.
These three parts work separately but I cannot make them work together. Particularly, my problem is how to input the System.Drawing.Bitmap of PART 1 into PART 3 which input is an Image<Bgr, Byte>.
So far I have tried to read the Bitmap "target" in Part 1 directly into Part 3 doing Image<Bgr, Byte> imageFrame = new Image<Bgr, Byte>(target) with no success (it doen't identify any contours)
I have also tried to create an intermediate .jpg file (Part 2) they can share with no success either (it doen't identify any contours either).
The only way that I can make this work is:
i) Run Part 1 and Part 2
ii) Open the resultant jpg image using Paint, hit "Save" and close Paint. I have done this manually.
iii) Run Part 3.
Doing this the contour is identified. However this is not a valid solutions since the step ii) is not automated. However this might help illustrating what the problem is.
Can someone help?
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
namespace Contouring
{
class Program
{
static void Main(string[] args)
{
//--------------------------------------PART 1 : DRAWING STUFF IN A BITMAP------------------------------------------------------------------------------------
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Bitmap bmp = new Bitmap(1000, 1000);
Graphics g = Graphics.FromImage(bmp);
//This is just an example using three rectangles for illustration purposes.
//In reality I have a set of arbitrary lines defining complex polygons.
g.DrawRectangle(blackPen, new Rectangle(10, 10, 200, 100)); //rectangle 1
g.DrawRectangle(blackPen, new Rectangle(20, 20, 50, 30)); //rectangle 2
g.DrawRectangle(blackPen, new Rectangle(200, 10, 25, 25)); //rectangle 3
Rectangle r = new Rectangle(10, 10, 250, 250); //bounding box of the 3 rectangles
Rectangle rcrop = new Rectangle(r.X, r.Y, r.Width + 10, r.Height + 10);//This is the cropping rectangle (bonding box adding 10 extra units width and height)
//Crop the model from the bmp
Bitmap src = bmp;
Bitmap target = new Bitmap(r.Width, r.Height);
using (Graphics gs = Graphics.FromImage(target))
{
gs.DrawImage(src, new Rectangle(5, 5, 250, 250), rcrop, GraphicsUnit.Pixel);
gs.Dispose();
}
//--------------------------------------PART 2 : SAVING THE BMP AS JPG------------------------------------------------------------------------------------
target.Save("test.jpg");
//--------------------------------------PART 3 : USING THE SAVED PICTURE AND FIND CONTOURS ----------------------------------------------------------------
Image<Bgr, Byte> imageFrame = new Image<Bgr, Byte>("test.jpg");
//Image<Bgr, Byte> imageFrame = new Image<Bgr, Byte>(target);
//Find contours
Image<Gray, byte> grayFrame = imageFrame.Convert<Gray, byte>();
List<Contour<Point>> result = new List<Contour<Point>>();
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
for (Contour<Point> contours = grayFrame.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
{
//here i do stuff with the contours and add them to the list result
result.Add(contours);
}
//Write to console
Console.WriteLine(result.Count + " NO. contours have been identified");
}//endmain
}//endprogram
}//endNamespace
Well, I think that trouble is in transparent. EmguCV doesn't understand images with transparent. I updated your code, so, now it work without saving. Does it help or you need transparent images?
//--------------------------------------PART 1 : DRAWING STUFF IN A BITMAP------------------------------------------------------------------------------------
var blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
var bmp = new Bitmap(1000, 1000, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
//This is just an example using three rectangles for illustration purposes.
//In reality I have a set of arbitrary lines defining complex polygons.
g.DrawRectangle(blackPen, new Rectangle(10, 10, 200, 100)); //rectangle 1
g.DrawRectangle(blackPen, new Rectangle(20, 20, 50, 30)); //rectangle 2
g.DrawRectangle(blackPen, new Rectangle(200, 10, 25, 25)); //rectangle 3
}
var r = new Rectangle(10, 10, 250, 250); //bounding box of the 3 rectangles
var rcrop = new Rectangle(r.X, r.Y, r.Width + 10, r.Height + 10);//This is the cropping rectangle (bonding box adding 10 extra units width and height)
//Crop the model from the bmp
var src = bmp;
var target = new Bitmap(r.Width, r.Height);
using (var gs = Graphics.FromImage(target))
{
gs.DrawImage(src, new Rectangle(5, 5, 250, 250), rcrop, GraphicsUnit.Pixel);
gs.Dispose();
}
//--------------------------------------PART 3 : USING THE SAVED PICTURE AND FIND CONTOURS ----------------------------------------------------------------
var imageFrame = new Image<Bgr, Byte>(target);
//Find contours
var grayFrame = imageFrame.Convert<Gray, byte>();
var result = new List<Contour<Point>>();
using (var storage = new MemStorage()) //allocate storage for contour approximation
for (var contours = grayFrame.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
{
//here i do stuff with the contours and add them to the list result
result.Add(contours);
}
On a single bitmap I need to display graphs and text values. So what I did is create a bitmap with points and creating a another bitmap with the text and place on the large bitmap.
I tried using the brush to write the text, but I am not able to see the underlying graphics even though trasparency is set.
Instead I thought to set the transparency for the text bitmap, but the bitmap which I have created are 24 bit rgb. So can we set the transparency for the 24 bit map.
Bitmap textBitmap = null;
textBitmap = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
using (Graphics memoryGrahics =
Graphics.FromImage(textBitmap))
{
memoryGrahics.FillRectangle(Brushes.Black, new Rectangle(0, 0, 100, 100));
memoryGrahics.DrawString(result, f, Brushes.White, x, y);
}
//placing the text bitmap on the graphbitmap
using (Graphics g = Graphics.FromImage(GraphBitmap))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
textBitmap.MakeTransparent();
g.DrawImage(textBitmap, 0, 0);
return GraphBitmap;
}
well.. it seems like you are using 2 different Graphical objects... although 1 Graphics objects with 1 bitmap can handle multiple layouts of custom drawings, like so:
int width = 800, height = 600;
var bit = new Bitmap(width, height);
var g = Graphics.FromImage(bit);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var area = new Rectangle(0, 0, width, height);
g.FillRectangle(new LinearGradientBrush(area, Color.PaleGoldenrod, Color.OrangeRed, 45), area);
g.DrawImage(Image.FromFile(#"your image"), new Point(10, 10));
g.DrawString("sample", new System.Drawing.Font("Tahoma", 56), new SolidBrush(Color.Black), new PointF(50, 50));
pictureBox1.Image = bit;
note that g.DrawImage method can be used to load other bitmaps as well
I perform a screen capture and get an image with a text in it. Let consider the text in the image to read - 'Hello'.
Now, I would like to 'create' an image from text 'Hello' which has all the same properties (font style, size, pixel format..) as the text - Hello from my captured image.
I'm using the following code to convert the string - Hello to an image.
string str = "Hello";
Bitmap bmp = new Bitmap(74, 16, PixelFormat.Format32bppArgb);
using (Graphics gfx = Graphics.FromImage((Image)bmp))
{
Font font = new Font("Tahoma", 11, FontStyle.Regular, GraphicsUnit.Pixel);
gfx.FillRectangle(Brushes.Transparent, new RectangleF(0, 0, bmp.Width, bmp.Height));
gfx.FillRectangle(Brushes.Black, 0, 0, 74, 16);
gfx.DrawString(str, font, new SolidBrush(Color.White), 1, 1);
bmp.Save(#"C:\temp\" + str + ".bmp", ImageFormat.Bmp);
}
The two images (one from screen capture, and second from creating) are not the same.
How do I create an image with text which would match exactly to the image with text from the screen capture ?
Here is the code I use to screen capture from the third party application....
Rectangle rect = new Rectangle(194, 41, 74, 16);
using (Bitmap bmpScreenShot = new Bitmap(rect.Width, rect.Height))
{
using (Graphics gfxScreenShot = Graphics.FromImage(bmpScreenShot))
{
gfxScreenShot.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmpScreenShot.Size, CopyPixelOperation.SourceCopy);
gfxScreenShot.Dispose();
MemoryStream imageStream = new MemoryStream();
bmpScreenShot.Save(imageStream, ImageFormat.Bmp);
bmpScreenShot.Save(#"c:\temp\pic1_0.bmp");
}
}
I assume you're trying to implement an OCR. I've never tried anything like it, and can imagine it get's quite complicated. You might want to check out other (open-source) OCRs, like:
OCR in the Cloud, by Microsoft
Tessnet2
I'm trying to create an image with a transparent background to display on a web page.
I've tried several techniques but the background is always black.
How can I create a transparent image and then draw some lines on it ?
Call Graphics.Clear(Color.Transparent) to, well, clear the image. Don't forget to create it with a pixel format that has an alpha channel, e.g. PixelFormat.Format32bppArgb. Like this:
var image = new Bitmap(135, 135, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(image)) {
g.Clear(Color.Transparent);
g.DrawLine(Pens.Red, 0, 0, 135, 135);
}
Assumes you're using System.Drawing and System.Drawing.Imaging.
Edit: Seems like you don't actually need the Clear(). Just creating the image with an alpha channel creates a blank (fully transparent) image.
This might help(something I threw together which sets the background of a Windows form to a transparent image:
private void TestBackGround()
{
// Create a red and black bitmap to demonstrate transparency.
Bitmap tempBMP = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(tempBMP);
g.FillEllipse(new SolidBrush(Color.Red), 0, 0, tempBMP.Width, tempBMP.Width);
g.DrawLine(new Pen(Color.Black), 0, 0, tempBMP.Width, tempBMP.Width);
g.DrawLine(new Pen(Color.Black), tempBMP.Width, 0, 0, tempBMP.Width);
g.Dispose();
// Set the transparancy key attributes,at current it is set to the
// color of the pixel in top left corner(0,0)
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(tempBMP.GetPixel(0, 0), tempBMP.GetPixel(0, 0));
// Draw the image to your output using the transparancy key attributes
Bitmap outputImage = new Bitmap(this.Width,this.Height);
g = Graphics.FromImage(outputImage);
Rectangle destRect = new Rectangle(0, 0, tempBMP.Width, tempBMP.Height);
g.DrawImage(tempBMP, destRect, 0, 0, tempBMP.Width, tempBMP.Height,GraphicsUnit.Pixel, attr);
g.Dispose();
tempBMP.Dispose();
this.BackgroundImage = outputImage;
}