I have data like this(x_pos, y_pos, z_pos, Length, height, depth). I need to draw cube by using this information. Can any one help me to do draw Teechart for this. give me some sample code for this.
Yes, you can do it with custom canvas drawing using the Cube(Rectangle r, int z0, int z1) method in TChart's AfterDraw event, for example:
void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
int x = 100;
int y = 100;
int z = 0;
int width = 50;
int height = 50;
int depth= 10;
DrawCube(x, y, z, width, height, depth, g);
}
private void DrawCube(int x, int y, int z, int width, int height, int depth, Steema.TeeChart.Drawing.Graphics3D g)
{
Rectangle r = new Rectangle(x, y, width, height);
g.Cube(r, z, z + depth);
}
Related
How can I get the color of an area that has 5x5px inside an image.
int xPixel = 200; int yPixel = 100;
Bitmap myBitmap = new Bitmap(“C:/Users/admin/Desktop/image.png");
Color pixelColor = myBitmap.GetPixel(xPixel, yPixel, 5, 5);
MessageBox.Show(pixelColor.Name);
This code does not work!.
You can use something this extension method to get dominant color in a region of an image in case they are not all the same
public static Color GetDominantColor(this Bitmap bitmap, int startX, int startY, int width, int height) {
var maxWidth = bitmap.Width;
var maxHeight = bitmap.Height;
//TODO: validate the region being requested
//Used for tally
int r = 0;
int g = 0;
int b = 0;
int totalPixels = 0;
for (int x = startX; x < (startX + width); x++) {
for (int y = startY; y < (startY + height); y++) {
Color c = bitmap.GetPixel(x, y);
r += Convert.ToInt32(c.R);
g += Convert.ToInt32(c.G);
b += Convert.ToInt32(c.B);
totalPixels++;
}
}
r /= totalPixels;
g /= totalPixels;
b /= totalPixels;
Color color = Color.FromArgb(255, (byte)r, (byte)g, (byte)b);
return color;
}
You can then use it like
Color pixelColor = myBitmap.GetDominantColor(xPixel, yPixel, 5, 5);
there is room for improvement, like using a Point and Size, or even a Rectangle
public static Color GetDominantColor(this Bitmap bitmap, Rectangle area) {
return bitmap.GetDominantColor(area.X, area.Y, area.Width, area.Height);
}
but this should be enough to get started.
A solution that uses the actual drawing methods provided by System.Drawing to resize the given area to 1x1 and get its pixel value:
public static Color GetRectangleColor(Bitmap sourceBitmap, Int32 x, Int32 y, Int32 width, Int32 height)
{
using(Bitmap onePix = new Bitmap(1,1, PixelFormat.Format24bppRgb))
{
using (Graphics pg = Graphics.FromImage(onePix)){
pg.DrawImage(sourceBitmap,
new Rectangle(0, 0, 1, 1),
new Rectangle(x, y, width, height)),
GraphicsUnit.Pixel);
return onePix.GetPixel(0, 0);
}
}
Though, if you are consistently working with squares of a uniform colour, I personally wouldn't bother. Just avoid any potential fades at the edges and you're fine:
public static Color GetRectangleCenterColor(Bitmap sourceBitmap, Int32 x, Int32 y, Int32 width, Int32 height)
{
return sourceBitmap.GetPixel(x + (width / 2), y + (height / 2));
}
i have a program which takes a screen shot of the selected area (which I select with a mouse) and saves it to a clipboard. The problem is it works only if i make a selection from top to bottom. If I try to make a selection in any other direction (bottom to top, right to left, left to right) the program crashes. This is the code for MouseMove:
public void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.isMouseDown)
{
double curx = e.GetPosition(null).X;
double cury = e.GetPosition(null).Y;
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
SolidColorBrush brush = new SolidColorBrush(Colors.White);
r.Stroke = brush;
r.Fill = brush;
r.StrokeThickness = 1;
r.Width = Math.Abs(curx - x);
r.Height = Math.Abs(cury - y);
selekt.Children.Clear();
selekt.Children.Add(r);
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
if (e.LeftButton == MouseButtonState.Released)
{
selekt.Children.Clear();
width = e.GetPosition(null).X - x;
height = e.GetPosition(null).Y - y;
this.CaptureScreen(x, y, width, height);
this.x = this.y = 0;
this.isMouseDown = false;
this.Close();
}
}
}
And this is for CaptureScreen:
public void CaptureScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
Bitmap slika = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(slika);
g.CopyFromScreen(ix, iy, 0, 0,new System.Drawing.Size(iw, ih),CopyPixelOperation.SourceCopy);
System.Windows.Forms.Clipboard.SetImage(slika);
are you getting error the below mentioned code? That seems to be the place where you would get one.
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
If yes, then thats because SetLeft takes UIElement and a double values
public static void SetLeft(
UIElement element,
double length)
And also, I am guessing that x and y are double and declared public.
I am trying to create textboxes and draw circles dynamically within a user control. The circle is visible but the textboxes are not visible when I run my application. Am I missing something in the code?
Please find the code below,
public partial class uscCircle : UserControl
{
public uscCircle()
{
InitializeComponent();
}
public void DrawCircle(PaintEventArgs args, int x, int y, int width, int height)
{
Pen pen = new Pen(Color.Red, 3);
Brush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
args.Graphics.FillEllipse(myBrush, x - width / 2, y - height / 2, width, height);
}
public void AddTextBox(string text, int x, int y, int width, int height)
{
markerlabel.Size = new Size(40, 15);
markerlabel.Text = text;
markerlabel.TextAlign = HorizontalAlignment.Center;
markerlabel.BorderStyle = BorderStyle.FixedSingle;
markerlabel.ForeColor = Color.White;
markerlabel.BackColor = Color.Red;
markerlabel.Location = new Point(x - (width + 14), y + height / 2);
markerlabel.Visible = true;
this.Controls.Add(markerlabel);
}
}
public partial class CalibrationForm : Form
{
private CalibrationForm_Click(object sender, EventArgs e)
{
int x = e.X;
int y = e.Y;
DrawTextBox(X, Y, 25, 25, "1234", "abcd");
}
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
PaintEventArgs arg = new PaintEventArgs(this.CreateGraphics(), new Rectangle());
uscCircle circle = new uscCircle();
circle.DrawCircle(arg, x, y, width, height);
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x + 40, y, width, height);
}
}
I don't see where you add uscCircle to the form. If that isn't displayed, neither will the textbox be.
Such as:
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
this.Controls.Add(circle);
}
You are adding your TextBox into a new User control which is then not used.
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
You either need to add your 'uscCircle' into the controls of the Form
this.Controls.Add(uscCircle); // Must be in your Form file
or you move the TextBox-Generation code into your form
Here is my code and I want to implement DDA algorithm without using drawLine method in c# . I tried to use PutPixel method but it did not work . There is nothing in my window. Is there any way to draw line without using drawLine method in c# ?
private void Form1_Paint(object sender, PaintEventArgs e)
{
grafik = e.Graphics;
DDACiz(ilk.X, ilk.Y, ikinci.X, ikinci.Y, grafik, Color.DarkRed);
}
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
bm.SetPixel(0, 0, Color.DarkRed);
g.DrawImageUnscaled(bm, x, y);
}
void DDACiz(int x1, int y1, int x2, int y2,Graphics grafik, Color renk)
{
int PikselSayisi;
int dx, dy;
float x, xFark;
float y, yFark;
dx = x2 - x1;
dy = y2 - y1;
PikselSayisi = Math.Abs(dx) > Math.Abs(dy) ? Math.Abs(dx) : Math.Abs(dy);
xFark = (float)dx / (float)PikselSayisi;
yFark = (float)dy / (float)PikselSayisi;
x = (float)x1;
y = (float)y1;
while (PikselSayisi!=0)
{
PutPixel(grafik,(int)Math.Floor(x + 0.5F),(int) Math.Floor(y + 0.5f),renk);
x += xFark;
y += yFark;
PikselSayisi--;
}
}
}
}
There is no Graphics.DrawPoint method, so to draw a single pixel with a Graphics object you need to use Graphics.FillRectangle
Change
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
bm.SetPixel(0, 0, Color.DarkRed);
g.DrawImageUnscaled(bm, x, y);
}
to
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
g.FillRectangle(Brushes.DarkRed, x, y, 1, 1);
}
or if you want to use the Color c:
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
using (SolidBrush brush = new SolidBrush(c) )
g.FillRectangle(brush , x, y, 1, 1);
}
You could also use your appoach of drawing a bitmap but you need to make it either 1x1 pixels wide or make sure it is transparent and also use CompositingMode.SourceOver.
Writing a line draw method is an interesting exercise; much harder than it seems and really tough for PenWidths other than 1.0, let alone for alpha channels other than fully opaque..
I often need to draw items in a Graphics object and the way I've been doing it is to have a function DrawItem that receives the Graphics object and an offsetX and offsetY parameters, which determine at which point the item will be drawn.
The problem is that the code inside DrawItem would look a lot better if there would be a method in Graphics that would give me a version of the Graphics where the X and Y axis zeroes are at some other point, something like myGraphics.DisplacedGraphics(offsetX, offsetY). This way I would just pass this Graphics objects to my DrawItem method which wouldn't need to receive the other two parameters. Is there such function or what's the closest thing?
Edit: On the meanwhile this is what I wrote, but seems like such a basic requirement I still hope there already exists such functionality (I'd still need to add a bunch of methods but these are all I need for now) (note the DisplacedCanvas method):
public class Canvas
{
private readonly Graphics _Graphics;
private readonly int _OriginX = 0;
private readonly int _OriginY = 0;
public Canvas(Graphics graphics, int originX, int originY)
{
_Graphics = graphics;
_OriginX = originX;
_OriginY = originY;
}
public Canvas(Graphics graphics) : this(graphics, 0, 0) { }
public SizeF MeasureString(string text, Font font)
{
return _Graphics.MeasureString(text, font);
}
public void FillRectangle(Brush brush, int x, int y, int width, int height)
{
_Graphics.FillRectangle(brush, _OriginX + x, _OriginY + y, width, height);
}
public void DrawString(string s, Font font, Brush brush, float x, float y)
{
_Graphics.DrawString(s, font, brush, _OriginX + x, _OriginY + y);
}
public Canvas DisplacedCanvas(int x, int y)
{
return new Canvas(_Graphics, _OriginX + x, _OriginY + y);
}
}
I'm pretty sure that the TranslateTransform() method will do what you're asking for.
The origin is typically the upper-left-hand corner of the drawing surface. The translation operation consists of multiplying the transformation matrix by a matrix whose translation part is the dx and dy parameters. This method applies the translation by prepending the translation matrix to the transformation matrix.
So if you want the new origin to be at 100, 50, then you would first call graphics.TranslateTransform(100, 50) before drawing your image.