I need to grey out disabled images in the asp page but I dont know how to achieve that
my current code is given below
<ul class="flag">
<li>
<img src="flag-china.png" alt="china" onclick="setLanguage('Chinese');" style="cursor:pointer"/></li>
<li>
<img src="flag-france.png" alt="france" onclick="setLanguage('French');" style="cursor:pointer" /></li>
<img src="us.png" alt="us" onclick="setLanguage('English');" style="cursor:pointer" /></li>
</ul>
I need to disable and grey out every flag except the last one
Found following code sample here. Call the gray out version when you need.
public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
Related
What am I trying to do?
The following function should simply return the image defined by parameter "sourceImage" with changed "opacity":
public static Image DisableImage(Image sourceImage)
{
Image newImage = new Bitmap(sourceImage);
using (Graphics g = Graphics.FromImage(newImage))
{
using (ImageAttributes imageAttributes = new ImageAttributes())
{
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = 0.5f;
imageAttributes.SetColorMatrix(colorMatrix);
//Draw the original image on the new image using the color matrix
g.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, imageAttributes);
}
}
return newImage;
}
What is the problem?
The returned image seems to be not changed at all.
What have I tried?
If changing the following line
ColorMatrix colorMatrix = new ColorMatrix();
to
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {Color.Red.R / 255.0f,
Color.Red.G / 255.0f,
Color.Red.B / 255.0f,
0, 1}
});
a red half-transparent "film" is drawn on the image, which makes me suspect that I missed something in how to use the ColorMatrix.
Any help is appreciated, thanks!
Alright, I figured it out myself.
Changing this line:
Image newImage = new Bitmap(sourceImage);
to
Image newImage = new Bitmap(sourceImage.Width, sourceImage.Height);
was the solution.
I was probably drawing on top of the existing image, which was of course not what I wanted.
Any possibility to delete this question?
I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).
That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.
I can't use fixed yellow circle image because the desired color is dynamic.
So in the code below, how can I change the color of the image before adding it to the bitmap?
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//--> Here I need to change the color of the green circle to yellow
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.
Fixed:
Thanks to #TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//Here I need to change the color of the green circle to yellow
i = ChangeToColor(b, Color.Gold)
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
While ChangeToColor function is as follows:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
This will create a new Bitmap with all non-transparent pixels moved strongly toward a new color:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1} // kudos to OP!
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
do make sure not to leak the Bitmaps you create!
Note that there are other methods as well. Here is a link to a method that uses ColorMapping. This allows for a range of colors to be replaced by another range, so it can keep gradients like the ones you get in anti-alised graphics..
Here's my solution you just need to create a new Control
then inherit the Picturebox check this out.
public partial class UICirclePicture : PictureBox
{
[Browsable(false)]
public int Depth { get; set; }
[Browsable(false)]
public SprikiwikiUI Ui
{
get { return SprikiwikiUI.Instance; }
}
[Browsable(false)]
public MouseState MouseState { get; set; }
public UICirclePicture()
{
BackColor = Ui.GetApplicationBackgroundColor();
SizeMode = PictureBoxSizeMode.StretchImage;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
using (var gp = new GraphicsPath())
{
gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
this.Region = new Region(gp);
}
}
}
Here is my code:
public static void ReduceScreenshot(string fileName)
{
var bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
var gfxSS = Graphics.FromImage(bmpSS);
gfxSS.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] { 1.5f, 1.5f, 1.5f, 0, 0},
new float[] { 1.5f,1.5f, 1.5f, 0, 0},
new float[] {1.5f, 1.5f, 1.5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {-1, -1, -1, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
Rectangle abc= new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height, GraphicsUnit.Pixel, attributes);
bmpSS.Save("ScreenshotGray.png", ImageFormat.Png);
}
}
It's how it work http://take.ms/6tzvU. How should i change area from rectangle to triangle?
Not sure just which points shall set up the triangle but the code to restrict DrawImage to it should look something like this:
using System.Drawing.Imaging;
..
..
Rectangle abc = new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
GraphicsPath gp = new GraphicsPath();
// three points to make up a triangle
// repeat the first one as one way of closing the path
// use your own coordinates!
Point[] p = new Point[] { new Point(12, 34), new Point(56, 78),
new Point(90, 12), new Point(12, 34) };
gp.AddLines(p); // or AddPolygon
gfxSS.SetClip(gp); // now restrict the Graphics object to the interior of the path
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height,
GraphicsUnit.Pixel, attributes);
What I'm trying to do is paint a solid color and/or pattern with some degree of opacity over an existing image. I believe from what I've read this will involve a bitmap mask. The examples I've seen using bitmap masks as opacity masks only show them used against images to crop them a certain way, and I want to use it for painting. Here is basically what I'm trying to accomplish:
The first image is being loaded and drawn onto a derived Canvas class using DrawImage. I'm trying to accomplish what you see in the 3rd image, the 2nd is an example of a mask I might use. The two key points being that the blue surface in the 3rd image needs to be any arbitrary color, and it needs some opacity so that you can still see the shading on the underlying image. This is kind of a simple example, some of the other objects have much more surface detail and much more complicated masks.
A color matrix can be useful here:
private Image tooth = Image.FromFile(#"c:\...\tooth.png");
private Image maskBMP = Image.FromFile(#"c:\...\toothMask.png");
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.DrawImage(tooth, Point.Empty);
using (Bitmap bmp = new Bitmap(maskBMP.Width, maskBMP.Height,
PixelFormat.Format32bppPArgb)) {
// Transfer the mask
using (Graphics g = Graphics.FromImage(bmp)) {
g.DrawImage(maskBMP, Point.Empty);
}
Color color = Color.SteelBlue;
ColorMatrix matrix = new ColorMatrix(
new float[][] {
new float[] { 0, 0, 0, 0, 0},
new float[] { 0, 0, 0, 0, 0},
new float[] { 0, 0, 0, 0, 0},
new float[] { 0, 0, 0, 0.5f, 0},
new float[] { color.R / 255.0f,
color.G / 255.0f,
color.B / 255.0f,
0, 1}
});
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorMatrix(matrix);
e.Graphics.DrawImage(bmp,
new Rectangle(Point.Empty, bmp.Size),
0,
0,
bmp.Width,
bmp.Height,
GraphicsUnit.Pixel, imageAttr);
}
}
The 0.5f value in the Matrix declaration is the alpha value.
I am trying to make to put adjust brightness thing but I am getting does not exist in current context error for "NewBitmap" in this code
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
Here is my code
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblBrightNum.Text = trackBar1.Value.ToString();
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
}
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
Bitmap TempBitmap = (Bitmap)Image.Clone();
float FinalValue = (float)Value / 255.0f;
Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;
}
Your program mainly contains two methods, one is trackBar1_Scroll, another one is AdjustBrightness, visual studio knows what is "NewBitmap" in the AdjustBrightness method, but it doesn't know what is "NewBitmap" in the trackBar1_Scroll
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblBrightNum.Text = trackBar1.Value.ToString();
//Visual studio is complaining about this, you haven't define "NewBitmap", you can fix by adding below:
Bitmap NewBitmap = //your bitmap
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
}
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
Bitmap TempBitmap = (Bitmap)Image.Clone();
float FinalValue = (float)Value / 255.0f;
Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;