Named argument specifications must appear after all fixed arguments have been specified - c#

I'm working in image processing in C# and I have two major error:
Error: Named argument specifications must appear after all fixed arguments have been specified
Error: System.Drawing.Size' is a 'type' but is used like a 'variable'
This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
using Emgu.CV.GPU;
using Emgu.CV.UI;
namespace SNAKE_C_Sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void imageBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Image image = Image.FromFile(dialog.FileName);
pictureBox1.Image = image;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
struct parameter
{
public double alpha { get; set; }
public double beta { get; set; }
public double gamma { get; set; }
};
unsafe private void button3_Click(object sender, EventArgs e)
{
{
int length = 1000;
MCvPoint2D64f* contour;
MCvPoint2D64f center = new MCvPoint2D64f();
var snake_param = new List<parameter>();
snake_param.Add(new parameter { alpha = 0.1, beta = 0.1, gamma = 0.1, });
//Image src_img = pictureBox1.Image;
IntPtr dst_img = new IntPtr();
//IntPtr src_img = Emgu.CV.CvInvoke.cvLoadImage("pictureBox1.Image", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
Bitmap bitmapp = new Bitmap("pictureBox1.Image");
Image<Bgr, byte> image = new Image<Bgr, byte>(bitmapp);
center.x = image.Width;
center.y = image.Height;
int i;
for (i = 0; i < length; i++)
{
contour[i].x = (int)(center.x * Math.Cos(2 * Math.PI * i / length) + center.x);
contour[i].y = (int)(center.y * Math.Sin(2 * Math.PI * i / length) + center.y);
}
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0);
}
CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
IntPtr src_img = image.Ptr;
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
CvInvoke.cvCvtColor(src_img, dst_img, COLOR_CONVERSION.GRAY2RGB);
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
}
CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap bitmappbb = new Bitmap("dst_img");
Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmapp);
pictureBox2.Image = bitmappbb;
}
}
private void imageBox1_Click_1(object sender, EventArgs e)
{
}
private void panAndZoomPictureBox1_Click(object sender, EventArgs e)
{
}
private void imageBox1_Click_2(object sender, EventArgs e)
{
}
}
}
How can i adjust above error?

This is one of the issues that caused error 1
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0);
I'll make it more readable...
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255, 0, 0),
2,
lineType: LINE_TYPE.EIGHT_CONNECTED,
0
);
See the 2nd-to-last line is using a named argument (lineType:), but is followed by a non-named argument? How is the compiler meant to know what you mean?
The 2nd error is as #LajosArpad stated, you need to add a new in front of your use of System.Drawing.Size(..).

Instead of
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
you need this:
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], new System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
This should fix the second error. Without the new keyword you do not have a System.Drawing.Size instance.
EDIT:
I am not going to test your code, nor to read it line-by-line, so I expect more information about your first error to give you the solution. Can you tell me on which line is the exception thrown?
Also, might I suggest that you should pay more attention to the tabulation of your code, as it is difficult to read if you write your code in such an unstructured manner. It is not impossible to read, but most of us (including myself) will not read it.

I fixed the last error and that's my new code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "JPEG|*.jpg|PNG|*.PNG";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Image image = Image.FromFile(dialog.FileName);
pictureBox1.Image = image;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
struct parameter
{
public float alpha { get; set; }
public float beta { get; set; }
public float gamma { get; set; }
};
unsafe private void button3_Click(object sender, EventArgs e)
{
{
int length = 1000;
Point *contour;
Point center = new Point();
var snake_param = new List<parameter>();
snake_param.Add(new parameter { alpha= 0.1f , beta = 0.1f, gamma= 0.1f, });
IntPtr dst_img= new IntPtr();
Bitmap bitmap = new Bitmap("pictureBox1.Image");
Image<Bgr, byte> image = new Image<Bgr, byte>(bitmap);
center.X = image.Width;
center.Y = image.Height;
int i;
for (i = 0; i < length; i++)
{
contour[i].X = (int)(center.X * Math.Cos(2 * Math.PI * i / length) + center.X);
contour[i].Y = (int)(center.Y * Math.Sin(2 * Math.PI * i / length) + center.Y);
}
LINE_TYPE lignetype = new LINE_TYPE();
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0 );
}
CvInvoke.cvLine
(
dst_img,
contour[length - 1],
contour[0],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0
);
IntPtr ctr =new IntPtr();
//public void PixelToInkSpace(
//IntPtr a
//ref Point contour
//);
IntPtr src_img = image.Ptr;
CvInvoke.cvSnakeImage(
src_img,
contour[i],
length,
snake_param.[1].alfa,
snake_param[2].beta,
snake_param[3].gamma,
1,
new System.Drawing.Size(15, 15),
new MCvTermCriteria(1,0.0),
1);
CvInvoke.cvCvtColor(
src_img,
dst_img,
COLOR_CONVERSION.GRAY2RGB );
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0 );
}
CvInvoke.cvLine(
dst_img,
contour[length - 1],
contour[0],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap bitmappbb = new Bitmap("dst_img");
Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmappbb);
pictureBox2.Image = bitmappbb;
}
}
}
}
But my error now is different as I'm translating my code from c++ to c# ,
I discover that the snake format is
public static void cvSnakeImage(
IntPtr image,
IntPtr points,
int length,
float[] alpha,
float[] beta,
float[] gamma,
int coeffUsage,
Size win,
MCvTermCriteria criteria,
bool calcGradient
)
I didn't find way to convert the variable "contour" with type "Point" to "IntPtr".
And a way to call alfa, beta et gamma as float[];
#Timothy Walters

Related

How to invoke Load_form function without using button

I am trying to create a GUI that shows small 3 images on the app. When there is a new image in the folder, the new image has to be on the right
side of the imageBoxes. In order to do that, I am using a timer to invoke Load_Form function. The interval is 1000ms. However after some time (
approximately 3:20 min), the program gives an exception "Out of memory" or "Parameter is not valid". Is there another way to update imageboxes automatically( when there is
new image, update it)? The code is here, thank you in advance:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
using System.Timers;
namespace test1
{
public partial class Form1 : Form
{
string actCode = null;
string pathLogNames = #"D:\LOG\Right";
string[] imPaths;
public Form1()
{
InitializeComponent();
//timer1.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
string[] getFiles(string path, string text, string fileExtension)
{
try
{
string searchingText = text;
searchingText = "*" + text + "*";
string[] filesArray = Directory.GetFiles(path, searchingText, SearchOption.AllDirectories);
List<string> filesList = new List<string>(filesArray);
List<string> newFilesList = new List<string>();
foreach (string file in filesList)
{
if (file.Contains(fileExtension) == true)
{
newFilesList.Add(file);
}
}
string[] files = newFilesList.ToArray();
return files;
}
catch
{
string[] files = new string[0];
return files;
}
}
string[] viewerPaths;
int pathsNumber;
Image actImage;
Image actImage1;
Image actImage2;
//int actIndex;
double imageDefaultZoom;
public bool mouseFlag = false;
//Point startPoint = new Point();
public void setPaths(string[] paths, double defZoom)
{
viewerPaths = paths;
pathsNumber = viewerPaths.Length;
imageDefaultZoom = defZoom;
int total = viewerPaths.Length;
if (pathsNumber > 0)
{
actImage = Image.FromFile(viewerPaths[total - 3]);
Bitmap b = new Bitmap(actImage);
Image i = resizeImage(b, new Size(100, 100));
pictureBox1.Image = i;
actImage1 = Image.FromFile(viewerPaths[total - 2]);
Bitmap b1 = new Bitmap(actImage1);
Image i1 = resizeImage(b1, new Size(100, 100));
pictureBox2.Image = i1;
actImage2 = Image.FromFile(viewerPaths[total - 1]);
Bitmap b2 = new Bitmap(actImage2);
Image i2 = resizeImage(b2, new Size(100, 100));
pictureBox3.Image = i2;
}
}
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
//Get the image current width
int sourceWidth = imgToResize.Width;
//Get the image current height
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//Calulate width with new desired size
nPercentW = ((float)size.Width / (float)sourceWidth);
//Calculate height with new desired size
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//New Width
int destWidth = (int)(sourceWidth * nPercent);
//New Height
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw image with new width and height
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
private void onChanged(object sender, FileSystemEventArgs e)
{
//Dispose();
imPaths = getFiles(pathLogNames, actCode, ".tif");
setPaths(imPaths, 0.5);
}
private void Form1_Load(object sender, EventArgs e)
{
imPaths = getFiles(pathLogNames, actCode, ".tif");
setPaths(imPaths, 0.5);
}
}
}
}

Centroid tracking issue on Aforge C#

I am creating a program that can track multiple objects and get their centroid eventually I would use these centroids to connect to the nearest centroid of another object. But my problem is, my program tracks only one significant object on the video and it doesn't seem to display the centroid of this object. anyone can help me?
namespace Video_Processing_fixed_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap video;
Graphics g;
bool OnOff;
int mode;
int thoigiandemnguoc = 5;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame+=new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
g = Graphics.FromImage(video2);
g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
g.Dispose();
if (mode == 1)
{
// create filter
EuclideanColorFiltering filter = new EuclideanColorFiltering();
// set center colol and radius
filter.CenterColor = Color.FromArgb(215, 30, 30);
filter.Radius = 100;
// apply the filter
filter.ApplyInPlace(video2);
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinWidth = 5;
blobcounter.MinHeight = 5;
blobcounter.FilterBlobs = true;
blobcounter.ObjectsOrder = ObjectsOrder.Area;
blobcounter.ProcessImage(video2);
Blob[] blobs = blobcounter.GetObjectsInformation();
AForge.Point Center = new AForge.Point();
if (blobs.Length > 0)
{
Center.X = blobs.Average(c => c.CenterOfGravity.X);
Center.Y = blobs.Average(c => c.CenterOfGravity.Y);
}
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach(Rectangle recs in rects)
if (rects.Length > 0)
{
foreach (Rectangle objectRect in rects)
{
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
{
graphic.DrawRectangle(pen, objectRect);
}
graphic.Dispose();
}
}
( Pen pen = new Pen(Color.White,3))
pictureBox2.Image = video2;
pictureBox1.Image = video;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning==true)
{
FinalFrame.SignalToStop();
FinalFrame.WaitForStop();
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void ButObjTrack_Click(object sender, EventArgs e)
{
mode = 1;
}
private void StopButt_Click(object sender, EventArgs e)
{
FinalFrame.SignalToStop();
}
}
}
You do not display the average center value. Change these lines:
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach(Rectangle recs in rects)
if (rects.Length > 0)
{
foreach (Rectangle objectRect in rects)
{
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
{
graphic.DrawRectangle(pen, objectRect);
}
graphic.Dispose();
}
}
To these:
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Graphics graphic = Graphics.FromImage(video2);
foreach (Rectangle objectRect in rects)
{
graphic.DrawRectangle(Pens.LightGreen, objectRect);
}
graphic.DrawRectangle(Pens.Red, Center.X - 4, Center.Y - 4, 8, 8);
graphic.Dispose();
}
}

Send drawing from a class to a form in C#

I have a class named CircleSector which draws a PIChart.
I am unable to draw the PIChart if I call the Class from form1.
Here is my code:
Form1:
public void button1_Click(object sender, EventArgs e)
{
int textdata = Convert.ToInt32(textBox1.Text);
CS = new CircleSector(textdata, this);
// CS.GetGraphicSector(this);
}
CircleSector:
public CircleSector(int TextData , Form1 D)
{
Pen CirclePen = new Pen(Color.Black);
Rect = new Rectangle(XAxis, YAxis, CircleRadius, CircleRadius);
float temp1 = 0;
SectorCircle = this.CreateGraphics();
PIVal = - 360 / TextData;
float temp2 = PIVal;
for (int i = 0; i <= TextData; i++)
{
m_Value = i;
SectorCircle.DrawPie(CirclePen, Rect, 0, temp2);
temp1 = temp2;
temp2 = temp2 - PIVal;
}
// MessageBox.Show("Mouse Pressed");
// return SectorCircle;
}
I think the problem is here.
SectorCircle = this.CreateGraphics();
try this.
SectorCircle = D.CreateGraphics();

picture box invalidate method in C#

I want to draw something in a picture box and remove them.
in this case i need to draw a fill circle and after a time remove it and draw a circle without fill it.
i use below code but when i want to remove a shape i need to draw picturebox another time with new shape and it causes a sensitive change in picture box.now i know i should use invalidate() method. but i do not know where and how should i use that.
void pbmapDo()
{
Graphics graphicPBMap = pbMap.CreateGraphics();
// usually Values : gridNeedUpdate = true; rulersNeedUpdate = true; rulerNeedUpdate = true; backGroundNeedUpdate = true; nodesNeedUpdate = true;
if (backGroundNeedUpdate)
{
Bitmap srce = new Bitmap(BackGround);
Bitmap dest = new Bitmap(pbMap.Width, pbMap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics gr = Graphics.FromImage(dest);
gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size));
graphicPBMap.DrawImage(dest, 0, 0);
}
backGroundNeedUpdate = false;
if (isGridShow && gridNeedUpdate)
{
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, 0, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width, i * 60, 0, i * 60);
}
}
gridNeedUpdate = false;
if (isShowRulers && rulersNeedUpdate )
{
graphicPBMap.DrawLine(new Pen(Brushes.Black, 46), pbMap.Width - 1, pbMap.Height - 1, 0 - 1, pbMap.Height - 1);
graphicPBMap.DrawLine(new Pen(Brushes.Black, 49), 0, 0, 0, pbMap.Height);
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawString(XPixelToLong((double)pbMap.Width - i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, pbMap.Width - i * 60, pbMap.Height - 15);
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, pbMap.Height - 24, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, 0, i * 60, 25, i * 60);
graphicPBMap.DrawString(YPixelToLat(i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, 0, i * 60);
}
}
rulersNeedUpdate = false;
if (rulerNeedUpdate)
{
if (x0ruler != -1 && y0ruler != -1)
{
if (x1ruler != -1 && y1ruler != -1)
{
Rectangle rectAngle = new Rectangle((int)(x1ruler - 1), (int)(y1ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
graphicPBMap.DrawLine(rulerPen, x0ruler, y0ruler, x1ruler, y1ruler);
rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
else
{
Rectangle rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
}
}
rulerNeedUpdate = false;
if (nodesNeedUpdate)
{
nodesNeedUpdate = false;
Node node;
for (int i = 0; i < nodes.Count; i++)
{
node = (Node)(nodes[i]);
if (node.IsOn)
{
drawOnCircle(graphicPBMap,node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
else
{
drawOffCircle(graphicPBMap, node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
}
}
}
EDIT 1 :
i changed code once.
You can use
invalidate() or pictureBox.image = null; where you need to change the image of your picturebox.
Anyway, it is not required to change the background always. Instead you can make your bitmap and place it as pictureBox.Image = bitmap.
If you need exact sample source code let me know.
sorry for the late.
try this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bckMap = null;
private void Form1_Load(object sender, EventArgs e)
{
bckMap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bckMap))
{
g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bckMap.Width, bckMap.Height));
g.Dispose();
}
pictureBox1.BackgroundImage = bckMap;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(ellips))
{
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(0, 0, ellips.Width, ellips.Height));
g.Dispose();
}
this.pictureBox1.Image = ellips;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(ellips))
{
g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(5, 5, ellips.Width-10, ellips.Height-10));
g.Dispose();
}
this.pictureBox1.Image = ellips;
}
I finally find a solution.
I need to use paint event of picture box.
private void pbMap_Paint(object sender, PaintEventArgs e)
{
//do every think you want with picture box like draw circle or change background
}
in this event i put every think i want to do with pbMap picture box.
and when i changed some i call below function to apply my changes,
public void updateMap()
{
if (pbMap.InvokeRequired) // just if you call in thread
{
pbMap.Invoke(new Action(() => pbMap.Invalidate()));
}
else
{
pbMap.Invalidate();
}
}

namespace doesn't exist

I would like to try out a code in Microsoft Visual C# Express Edition and I'm getting this error:
The type or namespace name 'Properties' does not exist in the namespace 'EducationalSuite.Core' (are you missing an assembly reference?)
I right click the Reference but I didn't find the "Properties" either the "EducationalSuite.Core".
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Resources;
namespace EducationalSuite.Core.Plugins
{
public delegate void RectangleItemClickedDelegate(Rectangle rect, int index);
public partial class GeoSafariItem : Control
{
protected List<Rectangle> lastFlashingItems = new List<Rectangle>();
protected int lastHeight = 0;
private Image imageFile = null;
protected List<Rectangle> hotspots = new List<Rectangle>();
protected Dictionary<int, string> textItems = new Dictionary<int, string>();
protected Dictionary<int, FileInfo> audioItems = new Dictionary<int, FileInfo>();
protected Rectangle lastRectangle;
protected int selectedIndex = 0;
protected int countItemsLeft = 6;
protected int countItemsRight = 6;
protected int imageOffsetTop = 0;
protected int imageOffsetBottom = 0;
protected bool paintHotSpots = false, colorSwitch = false, paintItemLabels = false;
protected Timer timer = new Timer();
public event RectangleItemClickedDelegate HotspotClick;
public event RectangleItemClickedDelegate QuestionItemClick;
public event RectangleItemClickedDelegate QuestionItemRightClick;
protected void OnHotspotClick(Rectangle rect, int index)
{
if (HotspotClick != null)
{
HotspotClick(this.RectangleToScreen(rect), index);
}
}
protected void OnQuestionItemRightClick(Rectangle rect, int index)
{
if (QuestionItemRightClick != null)
{
QuestionItemRightClick(this.RectangleToScreen(rect), index);
}
}
protected void OnQuestionItemClick(Rectangle rect, int index)
{
if (QuestionItemClick != null)
{
QuestionItemClick(this.RectangleToScreen(rect), index);
}
}
public GeoSafariItem()
{
this.imageFile = EducationalSuite.Core.Properties.Resources.singlepixel;
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;
this.MouseUp += new MouseEventHandler(GeoSafariItem_MouseUp);
// Activates double buffering
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.DoubleBuffered = true;
//InitializeComponent();
}
public void SetItemText(int index, string text)
{
if (string.IsNullOrEmpty(text))
{
if (this.textItems.ContainsKey(index)) textItems.Remove(index);
}
else
{
this.textItems[index] = text;
}
if (PaintItemLabels)
{
this.Invalidate();
}
}
public string GetItemText(int index)
{
if (this.textItems.ContainsKey(index))
{
return this.textItems[index];
}
else
{
return string.Empty;
}
}
public void SetItemAudio(int index, FileInfo file)
{
if ((file == null) && !file.Exists)
{
if (this.audioItems.ContainsKey(index)) audioItems.Remove(index);
}
else
{
this.audioItems[index] = file;
}
}
public FileInfo GetItemAudio(int index)
{
if (this.audioItems.ContainsKey(index))
{
return this.audioItems[index];
}
else
{
return null;
}
}
#region Recording Regions
bool isRecording = false;
int recordingIndex = 0;
Point recordTopLeft = Point.Empty;
Point recordBottomRight = Point.Empty;
List<Rectangle> recordedRectangles = new List<Rectangle>();
public void StartRecording()
{
isRecording = true;
recordingIndex = 0;
selectedIndex = 0;
recordedRectangles.Clear();
this.MouseUp += new MouseEventHandler(GeoSafariItemRecord_MouseUp);
this.Invalidate();
}
public List<Rectangle> FinishRecording()
{
isRecording = false;
this.MouseUp -= new MouseEventHandler(GeoSafariItemRecord_MouseUp);
this.Invalidate();
this.Hotspots.Clear();
foreach (Rectangle r in recordedRectangles)
{
this.Hotspots.Add(r);
}
return recordedRectangles;
}
private void GeoSafariItemRecord_MouseUp(object sender, MouseEventArgs e)
{
if (isRecording)
{
Rectangle size = SizeRect;
double ratio = (double)imageFile.Height / (double)size.Height;
if (recordTopLeft == Point.Empty)
{
recordTopLeft = new Point(
(int)(((double)e.Location.X - (double)size.Left) * ratio),
(int)(((double)e.Location.Y - (double)size.Top) * ratio)
);
}
else
{
recordBottomRight = new Point(
(int)(((double)e.Location.X - (double)size.Left) * ratio),
(int)(((double)e.Location.Y - (double)size.Top) * ratio)
);
Rectangle r = new Rectangle(recordTopLeft,
new Size(recordBottomRight.X - recordTopLeft.X, recordBottomRight.Y - recordTopLeft.Y));
this.recordedRectangles.Add(r);
recordingIndex++;
selectedIndex++;
recordTopLeft = Point.Empty;
recordBottomRight = Point.Empty;
}
}
this.Invalidate();
}
#endregion
void timer_Tick(object sender, EventArgs e)
{
colorSwitch = !colorSwitch;
if (lastRectangle.Width > 0)
{
this.Invalidate(lastRectangle);
}
else
{
this.Invalidate();
}
}
private Rectangle SizeRect
{
get
{
int rw, rh,
cw = (this.Width - 42),
ch = (this.Height - 2),
ox = 21,
oy = 1;
rw = cw;
rh = ch;
double imageRatio = (double)imageFile.Width / (double)imageFile.Height;
double controlRatio = (double)cw / (double)ch;
if (controlRatio > imageRatio)
{
rw = (int)Math.Round((double)rh * imageRatio);
ox += Math.Abs(rw - cw) / 2;
}
else if (controlRatio < imageRatio)
{
rh = (int)Math.Round((double)rw / imageRatio);
oy += Math.Abs(rh - ch) / 2;
}
return new Rectangle(ox, oy, rw, rh);
}
}
void GeoSafariItem_MouseUp(object sender, MouseEventArgs e)
{
Rectangle size = SizeRect;
for (int i = 0; i < hotspots.Count; i++)
{
Rectangle hotspot = hotspots[i];
double ratio = (double)size.Height / (double)imageFile.Height;
Rectangle adjustedRectange = new Rectangle(
size.Left + (int)(hotspot.X * ratio),
size.Top + (int)(hotspot.Y * ratio),
(int)(hotspot.Width * ratio),
(int)(hotspot.Height * ratio));
if (adjustedRectange.Contains(e.Location))
{
OnHotspotClick(hotspot, i);
return;
}
}
for (int i = 0; i < lastFlashingItems.Count; i++)
{
if (lastFlashingItems[i].Contains(e.Location))
{
if (e.Button == MouseButtons.Right)
OnQuestionItemRightClick(lastFlashingItems[i], i);
else
OnQuestionItemClick(lastFlashingItems[i], i);
return;
}
}
}
public List<Rectangle> Hotspots
{
get { return hotspots; }
}
public Image ImageFile
{
get { return imageFile; }
set
{
imageFile = value;
lastFlashingItems.Clear();
this.Invalidate();
}
}
public int SelectedIndex
{
get { return selectedIndex; }
set { selectedIndex = value; this.Invalidate(); }
}
public int CountItemsLeft
{
get { return countItemsLeft; }
set
{
countItemsLeft = value;
lastFlashingItems.Clear();
this.Invalidate();
}
}
public int CountItemsRight
{
get { return countItemsRight; }
set
{
countItemsRight = value;
lastFlashingItems.Clear();
this.Invalidate();
}
}
public int ImageOffsetTop
{
get { return imageOffsetTop; }
set
{
imageOffsetTop = value;
lastFlashingItems.Clear();
this.Invalidate();
}
}
public int ImageOffsetBottom
{
get { return imageOffsetBottom; }
set
{
imageOffsetBottom = value;
lastFlashingItems.Clear();
this.Invalidate();
}
}
public bool PaintHotSpots
{
get { return paintHotSpots; }
set { paintHotSpots = value; this.Invalidate(); }
}
public bool PaintItemLabels
{
get { return paintItemLabels; }
set { paintItemLabels = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
string itemText;
SizeF sizeItemText;
double topOffset = imageOffsetTop;
double bottomOffset = imageOffsetBottom;
double topOffsetPct = (double)topOffset / (double)imageFile.Height;
double bottomOffsetPct = (double)bottomOffset / (double)imageFile.Height;
Rectangle size = SizeRect;
SolidBrush brush = new SolidBrush(this.BackColor);
g.FillRectangle(brush, 0, 0, this.Width - 1, this.Height - 1);
g.FillRectangle(Brushes.Ivory, size.X - 25, size.Y, size.Width + 50, size.Height);
g.DrawRectangle(Pens.DarkKhaki, size.X - 25, size.Y - 1, size.Width + 50, size.Height + 1);
g.DrawImage(imageFile, size.X, size.Y, size.Width, size.Height);
Rectangle rect, rectItemText;
Brush selectedColor = (colorSwitch ? Brushes.Crimson : Brushes.Red);
topOffset = topOffsetPct * size.Height;
bottomOffset = bottomOffsetPct * size.Height;
int tmpHeight = (size.Height - (int)topOffset - (int)bottomOffset) / countItemsLeft;
if (size.Height != this.lastHeight || this.lastFlashingItems.Count == 0)
{
lastHeight = size.Height;
lastFlashingItems.Clear();
int actualIndex = 0;
for (int i = 0; i < countItemsLeft; i++)
{
int yy = size.Y + (tmpHeight * i) + (int)topOffset;
int xx = size.X - 18;
rect = new Rectangle(xx, yy, 16, 8);
this.lastFlashingItems.Add(rect);
g.FillRectangle((actualIndex == selectedIndex ? selectedColor : Brushes.Khaki), rect);
g.DrawRectangle(Pens.DarkKhaki, rect);
if (actualIndex == selectedIndex)
{
lastRectangle = rect;
}
itemText = this.GetItemText(actualIndex);
if (PaintItemLabels && !string.IsNullOrEmpty(itemText))
{
// Draw Text next to each notch
sizeItemText = g.MeasureString(itemText, this.Font);
int xxx = size.X + 10;
rectItemText = new Rectangle(xxx, yy, Convert.ToInt32(sizeItemText.Width), Convert.ToInt32(sizeItemText.Height));
PaintHotspot(g, Color.White, rectItemText, 200);
g.DrawString(itemText, this.Font, Brushes.Black, (float)xxx, (float)yy);
}
actualIndex++;
}
tmpHeight = (size.Height - (int)topOffset - (int)bottomOffset) / countItemsRight;
for (int i = 0; i < countItemsRight; i++)
{
int yy = size.Y + (tmpHeight * i) + (int)topOffset;
int xx = size.X + size.Width + 2;
rect = new Rectangle(xx, yy, 16, 8);
this.lastFlashingItems.Add(rect);
g.FillRectangle((actualIndex == selectedIndex ? selectedColor : Brushes.Khaki), rect);
g.DrawRectangle(Pens.DarkKhaki, rect);
if (actualIndex == selectedIndex)
{
lastRectangle = rect;
}
itemText = this.GetItemText(actualIndex);
if (PaintItemLabels && !string.IsNullOrEmpty(itemText))
{
// Draw Text next to each notch
sizeItemText = g.MeasureString(itemText, this.Font);
int xxx = size.X + size.Width - 10 - Convert.ToInt32(sizeItemText.Width);
rectItemText = new Rectangle(xxx, yy, Convert.ToInt32(sizeItemText.Width), Convert.ToInt32(sizeItemText.Height));
PaintHotspot(g, Color.White, rectItemText, 200);
g.DrawString(itemText, this.Font, Brushes.Black, (float)xxx, (float)yy);
}
actualIndex++;
}
}
else
{
lastHeight = size.Height;
for (int i = 0; i < lastFlashingItems.Count; i++)
{
g.FillRectangle((i == selectedIndex ? selectedColor : Brushes.Khaki), lastFlashingItems[i]);
g.DrawRectangle(Pens.DarkKhaki, lastFlashingItems[i]);
if (i == selectedIndex)
{
lastRectangle = lastFlashingItems[i];
}
}
if (PaintItemLabels)
{
int actualIndex = 0;
for (int i = 0; i < countItemsLeft; i++)
{
itemText = this.GetItemText(actualIndex);
if (!string.IsNullOrEmpty(itemText))
{
int yy = size.Y + (tmpHeight * i) + (int)topOffset;
// Draw Text next to each notch
sizeItemText = g.MeasureString(itemText, this.Font);
int xxx = size.X + 10;
rectItemText = new Rectangle(xxx, yy, Convert.ToInt32(sizeItemText.Width), Convert.ToInt32(sizeItemText.Height));
PaintHotspot(g, Color.White, rectItemText, 200);
g.DrawString(itemText, this.Font, Brushes.Black, (float)xxx, (float)yy);
}
actualIndex++;
}
tmpHeight = (size.Height - (int)topOffset - (int)bottomOffset) / countItemsRight;
for (int i = 0; i < countItemsRight; i++)
{
itemText = this.GetItemText(actualIndex);
if (!string.IsNullOrEmpty(itemText))
{
int yy = size.Y + (tmpHeight * i) + (int)topOffset;
// Draw Text next to each notch
sizeItemText = g.MeasureString(itemText, this.Font);
int xxx = size.X + size.Width - 10 - Convert.ToInt32(sizeItemText.Width);
rectItemText = new Rectangle(xxx, yy, Convert.ToInt32(sizeItemText.Width), Convert.ToInt32(sizeItemText.Height));
PaintHotspot(g, Color.White, rectItemText, 200);
g.DrawString(itemText, this.Font, Brushes.Black, (float)xxx, (float)yy);
}
actualIndex++;
}
}
}
// Calling the base class OnPaint
base.OnPaint(pe);
if (this.isRecording)
{
for (int i = 0; i < this.recordedRectangles.Count; i++)
{
rect = recordedRectangles[i];
double ratio = (double)size.Height / (double)imageFile.Height;
Rectangle adjustedRectange = new Rectangle(
size.Left + (int)(rect.X * ratio),
size.Top + (int)(rect.Y * ratio),
(int)(rect.Width * ratio),
(int)(rect.Height * ratio));
PaintHotspot(g, Color.LightBlue, adjustedRectange, (i + 1).ToString());
}
}
else if (this.paintHotSpots)
{
for (int i = 0; i < hotspots.Count; i++)
{
Rectangle hotspot = hotspots[i];
double ratio = (double)size.Height / (double)imageFile.Height;
Rectangle adjustedRectange = new Rectangle(
size.Left + (int)(hotspot.X * ratio),
size.Top + (int)(hotspot.Y * ratio),
(int)(hotspot.Width * ratio),
(int)(hotspot.Height * ratio));
PaintHotspot(g, Color.LightGreen, adjustedRectange, (i + 1).ToString());
}
}
}
protected virtual void PaintHotspot(Graphics g, Color c, Rectangle hotspot, int alpha)
{
PaintHotspot(g, c, hotspot, alpha, null);
}
protected virtual void PaintHotspot(Graphics g, Color c, Rectangle hotspot, string txt)
{
PaintHotspot(g, c, hotspot, 100, txt);
}
protected virtual void PaintHotspot(Graphics g, Color c, Rectangle hotspot, int alpha, string txt)
{
SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, c));
g.FillRectangle(brush, hotspot);
if (!string.IsNullOrEmpty(txt))
g.DrawString(txt, this.Font, Brushes.DarkGreen, hotspot.Location);
}
}
}
Update
I imagine the following line is causing the error.
this.imageFile = EducationalSuite.Core.Properties.Resources.singlepixel;
The code is referring to a image resource "singlepixel". This image must be in the default resource file of the EducationalSuite.Core assembly. First confirm that you are currently editing the said assembly by opening Project Properties and checking the Default Namespace on the Application page. This should state "EducationalSuite.Core". If this isn't the case, you are most likely missing a reference to the said assembly.
If you have the EducationalSuite.Core project open the easiest way to add the singlepixel resource is to open project properties, Resources tab and creating a new default resource file. From the top open the Add Resource drop down and select existing file or new image depending on whether you have the file already or if you need to create it. Name the resource "singlepixel".
Visual Studio will generate Resources helper class under Properties namespace for you so you can access the resource through the Properties.Resources.singlepixel under EducationalSuite.Core in your code.
Old answer
In general Properties namespace is the namespace which contains application or user specific settings. You can add these settings (and the namespace) by navigating to the Settings tab in the Properties of the project.
Unfortunately it's kind of hard to say more based on this information. Could you provide the piece of code that causes this error?
If you double click the error message the IDE will take you to the line which is causing the error.
Most likely the piece of code is expecting a setting variable which is not added to the project.
Looks like you are missing the Reference. If it is not under References in solution explorer than I would do a file search in windows for "EducationalSuite.Core" to see where it is on the system and add it. You may also be missing the "using" statement? If you hover over the "Properties" text you should get a small drop down that will add the using's for you.
If this does not help, more information would be helpful?
Hope this helps!

Categories

Resources