Draw transparent rectangles on image - c#

I want to create an overlay on an image, similar to the one in the picture.
Creating the translucent cover is easy, I just create a rectangle the size of the image and give it a low alpha value.
How can I get the transparent "cut-outs" though? Drawing over the image again just increases its alpha value. I need to explicitly set it to zero.
var image = picOriginal.Image;
var graphics = Graphics.FromImage(image);
var color = Color.FromArgb(200, Color.Black);
var brush = new SolidBrush(color);
var transparentColor = Color.Transparent;
var transparentBrush = new SolidBrush(transparentColor);
var rectangle = new Rectangle(Point.Empty, image.Size);
graphics.FillRectangle(brush, rectangle);
graphics.FillEllipse(transparentBrush, 100, 100, 100, 150);
var bitmap = new Bitmap(image);
picGenerated.Image = bitmap;

Related

Wide image not displaying on tablet

I use canvas and bitmap for create image.
I add my image in ScrollView (600x400).
int width = 2048;
int height = 600;
ImageView myImage = new ImageView(context);
Bitmap bitmapChart = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
Canvas canvasChart = new Canvas(bitmapChart);
var paint = new Paint();
paint.Color = Color.Red;
paint.SetStyle(Paint.Style.Fill);
Rect rect = new Rect(0, 0, width, height);
canvasChart.DrawRect(rect, paint);
myImage.SetImageBitmap(bitmapChart);
HorizontalScrollView scrollView1 = new HorizontalScrollView(context);
scrollView1.LayoutParameters.Height = 400;
scrollView1.LayoutParameters.Width = 600;
scrollView1.AddView(myImage);
this.AddView(scrollView1);
But if width of my image will be 2049 or more, this image will not displaying on tablet (on emulator it works normally).
Why I can't see my image?

How can i set a background image to a image which in being generated dynamically

I am creating some dynamic image using the following code
System.Drawing.Image img = new Bitmap(300, 600);
Graphics drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(System.Drawing.Color.White);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
Instead of white backgroung i want to use an image (company logo) so that it act like a water mark. How can i do that?
Change your textBrush definition to look ike this:
int alpha = 128; // range from 0 (transparent) to 255 (opaque)
Brush textBrush = new SolidBrush(Color.FromArgb(alpha, textColor);

Replace colors in indexed image using C#

I'm using Graphics.DrawImage to write a index bitmap into another image. The color black in the indexed image should be replaced with a transparent color when written to the "targetBitmap"
How do I do this in a nice performant way ?
var graphics = Graphics.FromImage(targetBitmap);
//I want the black color in "indexBitmap" to be transparent when it's written to "targetBitmap"
graphics.DrawImage(indexedBitmap,...)
Creating a color map and passing it as an "ImageAttributes" argument to DrawImage worked for me
var colorMaps = new[]{
new ColorMap {OldColor = Color.FromArgb(255, 0, 0, 0), NewColor = Color.Transparent}
};
var attr = new ImageAttributes();
attr.SetRemapTable(colorMaps);
How about using SetColorKey?
SetColorKey allows you to choose a transparent background when drawing the image on a graphics object.
But the fact is that this doesn't work when the SetRemapTable() function has been called.
You can make that work also by adding an extra ColorMap in the "colorMaps" array.
This extra ColorMap should have
- OldColor = 'Choose the transparent color'
- NewColor = Color.Transparent
and then call the SetRemapTable with the extended sub.
Below you see a code sample in C# to easily draw an image to a graphics object.
I use this to make a game with graphics.
This void (sub in basic) allows you to draw an image to graphics (eg. FormX.CreateGraphics()).
You can replace certain colors by other colors, and also choose a transparent color.
You can also draw the image with a specified angle (degrees).
public static void DrawImageToGraphics(Graphics gr, Bitmap img, Rectangle DestRect, Color[] OldColors, Color[] NewColors, Color TransparantColor, uint Angle)
{
System.Drawing.Drawing2D.Matrix lmx = new System.Drawing.Drawing2D.Matrix();
lmx.RotateAt(Angle, new PointF((DestRect.Left + DestRect.Right) / 2, (DestRect.Top + DestRect.Bottom) / 2));
gr.Transform = lmx;
System.Drawing.Imaging.ColorMap[] maps = new System.Drawing.Imaging.ColorMap[OldColors.Count() + 1];
for (int i = 0; i < OldColors.Count(); i++)
{
maps[i].OldColor = OldColors[i];
maps[i].NewColor = NewColors[i];
}
maps[OldColors.Count()].OldColor = TransparantColor;
maps[OldColors.Count()].NewColor = Color.Transparent;
System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
attr.SetRemapTable(maps);
gr.DrawImage(img, DestRect, 0, 0, img.Width, img.Height, GraphicsUnit.Point, attr);
}

How to draw centered text onto a jpg using system.drawing in c#

I am using the following code to draw text onto a jpg image but it requires x/y coordinate percision on where to place the text.
var bmp = new Bitmap("C:\\testing\\Given.jpg");
var gra = Graphics.FromImage(bmp);
var text = "The Berman's";
var font = new Font("Segoe Script", 24);
var brush = Brushes.Orange;
var point = new PointF(130, 224);
gra.DrawString(text, font, brush, point);
bmp.Save("C:\\testing\\Custom.jpg");
How would I go about centering text onto an image? I am guessing it would have to do with defining some sort of container (rectangle maybe?) that is the width of the image and centering the text within that? Not sure what the best practice would be for this.
using(var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
})
{
gra.DrawString(text, font, brush, new Rectangle(0, 0, bmp.Width, bmp.Height), sf);
}

create polygon filled up with dots

Am using the below code to create polygon. i just want to fill this polygon surface with black dots, how i can do that, then i want to convert this polygon to bitmap or in memory stream, how to do this??
// Create a blue and a black Brush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Transparent;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Polygon
Polygon yellowPolygon = new Polygon();
yellowPolygon.Stroke = blackBrush;
yellowPolygon.Fill = yellowBrush;
yellowPolygon.StrokeThickness = 4;
// Create a collection of points for a polygon
System.Windows.Point Point1 = new System.Windows.Point(50, 100);
System.Windows.Point Point2 = new System.Windows.Point(200, 100);
System.Windows.Point Point3 = new System.Windows.Point(200, 200);
System.Windows.Point Point4 = new System.Windows.Point(300, 30);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
polygonPoints.Add(Point4);
// Set Polygon.Points properties
yellowPolygon.Points = polygonPoints;
// Add Polygon to the page
mygrid.Children.Add(yellowPolygon);
Do the dots have to be positioned in a particular order or do you just want to have a dotted pattern in your polygon without specific order?
If you don't need a special order you could use a Brush, a DrawingBrush for instance. Check out this link: http://msdn.microsoft.com/en-us/library/aa970904.aspx
You can then set this Brush as the Fill-Property of your Polygon instead of the SolidColorBrush.
This is the DrawingBrush example from the msdn link, but modified to display dots:
// Create a DrawingBrush and use it to
// paint the rectangle.
DrawingBrush myBrush = new DrawingBrush();
GeometryDrawing backgroundSquare =
new GeometryDrawing(
Brushes.Yellow,
null,
new RectangleGeometry(new Rect(0, 0, 100, 100)));
GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new EllipseGeometry(new Rect(0, 0, 20, 20)));
SolidColorBrush checkerBrush = new SolidColorBrush(Colors.Black);
GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);
DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);
myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.05, 0.05);
myBrush.TileMode = TileMode.Tile;
yellowPolygon.Fill = myBrush;

Categories