C# Graphics Object is currently in use elsewhere - c#

I created a game based on a totorial to start learning, I am rendering my game with renderScreen(), when I moved my window on screen I have got this exception "Object is currently in use elsewhere". I know the reason of error but I could not fix it as I do not know how to do that.
What can I do ? Forgive me if question is stupid.
engine.cs
//MEMBERS
public static Graphics drawHandle;
private Thread renderThread;
public engine(Graphics g)
{
drawHandle = g;
}
private object bufferLock = new object();
public void init()
{
renderThread = new Thread(new ThreadStart(renderScreen));
renderThread.Start();
}
public void stop()
{
renderThread.Abort();
}
public static Bitmap frame = new Bitmap(Globals.CANVAS_WIDTH, Globals.CANVAS_HEIGHT);
Rectangle rect = new Rectangle(0, 0, frame.Width, frame.Height);
private void renderScreen()
{
int framesRendered = 0;
long startTime = Environment.TickCount;
Graphics frameGraphics = Graphics.FromImage(frame);
Unit1 unit1 = new Unit1("test1");
Unit1 unit2 = new Unit1("test2");unit2.x = 250; unit2.y = 100;
Globals.GameUnitsList.Add(unit1); Globals.GameUnitsList.Add(unit2);
while (true)
{
//BACKGROUND
frameGraphics.FillRectangle(new SolidBrush(Color.DarkBlue),0,0, Globals.CANVAS_WIDTH, Globals.CANVAS_HEIGHT);
//BOXES
Rectangle rect = new Rectangle(unit1.x, unit1.y, 50, 50);
unit1.rectangle = rect;
unit1.fillColor = Brushes.LightBlue;
unit1.hitPoints = 100;
unit1.Health = 10;
frameGraphics.FillRectangle(unit1.fillColor, unit1.rectangle);
frameGraphics.FillRectangle(unit1.fillColor, unit1.rectangle);
Rectangle rect2 = new Rectangle(unit2.x, unit2.y, 50, 50);
unit2.rectangle = rect2;
unit2.fillColor = Brushes.LightBlue;
unit2.hitPoints = 100;
unit2.Health = 50;
frameGraphics.FillRectangle(unit2.fillColor, unit2.rectangle);
frameGraphics.FillRectangle(unit2.fillColor, unit2.rectangle);
//HEALTH BAR
HealthBar hb = new HealthBar();
hb.barColor = Brushes.YellowGreen;
hb.damageColor = Brushes.Red;
frameGraphics.FillRectangle(hb.damageColor, hb.Draw(unit1, unit1.x, unit1.y)[1]);
frameGraphics.FillRectangle(hb.barColor, hb.Draw(unit1, unit1.x, unit1.y)[0]);
HealthBar hb2 = new HealthBar();
hb2.barColor = Brushes.YellowGreen;
hb2.damageColor = Brushes.Red;
frameGraphics.FillRectangle(hb2.damageColor, hb2.Draw(unit2, unit2.x, unit2.y)[1]);
frameGraphics.FillRectangle(hb2.barColor, hb2.Draw(unit2, unit2.x, unit2.y)[0]);
//UNIT NAMES
frameGraphics.DrawString(unit1.unitName, new Font("Arial", 24, FontStyle.Bold), Brushes.Yellow, unit1.x + 50, unit1.y + 50);
frameGraphics.DrawString(unit2.unitName, new Font("Arial", 24, FontStyle.Bold), Brushes.Yellow, unit2.x + 50, unit2.y + 50);
drawHandle.DrawImage(frame, 0, 0);
//BENCHMARKING
if (Environment.TickCount >= startTime + 750)
{
framesRendered++;
}
if (Environment.TickCount>=startTime+1000)
{
Console.WriteLine("Gomi-Invade Plan Game:"+framesRendered +"fps "+ Environment.TickCount);
framesRendered = 0;
startTime = Environment.TickCount;
}
}
}
gamewindow.cs
...
public static GameWindow gameW;
Game game = new Game();
...
//I have this code inside it but is this wrong ?
private void canvas_Paint(object sender, PaintEventArgs e)
{
Graphics g = canvas.CreateGraphics();
game.startGraphics(g);
}
...
game.cs
private engine Gengine;
public void startGraphics(Graphics g)
{
Gengine = new GomiEngine(g);
Gengine.init();
}
public void stopGame()
{
Gengine.stop();
}

You cannot render screen in your own thread, you need to call invalidate in invoke which will enqueue windows message to windows message loop which will be processed on main thread.
private void DoWorkInThread(object sender, EventArgs e)
{
while (true)
{
lock(this)
{
//update member properties
}
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate void ()
{
//this.Invalidate(); //if you don't need to render every frame, invalidate is better then refresh
this.Refresh(); //call refresh for immediate update
});
}
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
lock(this)
{
//read your member which are modifies by the thread
}
//do rendering
e.Graphics.Draw(...);
}

Related

WriteableBitmap performance: Debugging faster then Without Debugging

I have this code to measure the performance to draw a single pixel in a WriteableBitmap, and I notice in my system a considerable performance difference between these two starting modes, both in Release configuration.
When starting in Debugging, the speed is ~ 8.6K pixels/second, and when starting without Debugging ~ 2.6K. I was expecting the opposite
I wonder if I'm missing something or what is possible to do to achieve the same performance when starting in Debugging, or it is a bug.
class App1
{
[STAThread]
static void Main()
{
new Window1().Show();
new Application().Run();
}
}
class Image1 : Image
{
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { }
}
class Window1 : Window
{
WriteableBitmap writeableBitmap;
Stopwatch clock1;
long t1;
int frames_counter;
bool frame;
int px;
int py;
Int32Rect single_pixel = new Int32Rect(0, 0, 1, 1);
byte[] single_pixel_array;
readonly byte[] red_collor = new byte[] { 0, 0, 255, 0 }; // B G R
readonly byte[] blue_collor = new byte[] { 255, 0, 0, 0 }; // B G R
public Window1()
{
Height = 150;
Width = 350;
Loaded += Window1_Loaded;
}
private void Window1_Loaded(object sender, RoutedEventArgs e)
{
writeableBitmap = new WriteableBitmap((int)ActualWidth, (int)ActualHeight, 96, 96, PixelFormats.Bgr32, null);
Content = new Image1
{
Stretch = Stretch.None,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Source = writeableBitmap
};
single_pixel_array = red_collor;
frame = true;
px = 0;
py = 0;
var timer2 = new DispatcherTimer(new TimeSpan(0, 0, 0, 1), DispatcherPriority.Background, Callback2, Dispatcher.CurrentDispatcher);
var timer1 = new DispatcherTimer();
timer1.Tick += new EventHandler(Callback1);
timer1.Start();
clock1 = Stopwatch.StartNew();
t1 = clock1.ElapsedTicks;
}
private void Callback2(object sender, EventArgs e)
{
var t2 = clock1.ElapsedTicks;
var fps = 10000000F / (t2 - t1) * frames_counter;
Title = $"{fps:F1} fps";
frames_counter = 0;
t1 = t2;
}
private void Callback1(object sender, EventArgs e)
{
writeableBitmap.WritePixels(single_pixel, single_pixel_array, 4, px, py);
px++;
if (px > 100)
{
px = 0;
py++;
}
if (py > 100)
{
py = 0;
single_pixel_array = frame ? blue_collor : red_collor;
frame = !frame;
}
frames_counter++;
}
} enter code here

Resize rectangle with form Size in real time?

Here is the following rectangle below:
When I resize the form, I need this rectangle to match the size of the form.
When changing the width of the rectangle, do not interfere with its visibility within the form.
I'm using the following:
Note:
I did the rectangle manually, but if you have rectangle ready, better yet!
public Form1()
{
InitializeComponent();
this.Paint += Form1_Paint;
this.rectangles = new Dictionary<string, Rectangle>();
this.sizeScreen = this.Size;
this.sizeRectangles = new Size(8, 8);
this.brush = new SolidBrush(Color.Red);
FillLeft();
FillRight();
FillUp();
FillDown();
}
private Size sizeScreen;
private Size sizeRectangles;
private SolidBrush brush;
private Dictionary<string, Rectangle> rectangles;
private void FillLeft()
{
Rectangle rectangle = new Rectangle()
{
Height = this.sizeScreen.Height,
Width = this.sizeRectangles.Width,
X = 0,
Y = this.sizeRectangles.Height
};
this.rectangles.Add("left", rectangle);
}
private void FillRight()
{
Rectangle rectangle = new Rectangle()
{
Height = this.sizeScreen.Height,
Width = this.sizeRectangles.Width,
X = this.sizeScreen.Width - (this.sizeRectangles.Width * 5),
Y = this.sizeRectangles.Height
};
this.rectangles.Add("right", rectangle);
}
private void FillUp()
{
Rectangle rectangle = new Rectangle()
{
Height = this.sizeRectangles.Height,
Width = this.sizeScreen.Width,
X = 0,
Y = this.sizeRectangles.Height
};
this.rectangles.Add("up", rectangle);
}
private void FillDown()
{
Rectangle rectangle = new Rectangle()
{
Height = this.sizeRectangles.Height,
Width = this.sizeScreen.Width,
X = 0,
Y = this.sizeScreen.Height - (this.sizeRectangles.Height * 11)
};
this.rectangles.Add("down", rectangle);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < this.rectangles.Count; i++)
{
e.Graphics.FillRectangles(this.brush, this.rectangles.Values.ToArray());
}
}
I want to set the rectangle on the form when it is resized
This is the way I'm creating the rectangle, but it does not stay right on the screen, to resize it I do not know
I think this would simplify what you are trying to do:
const int PenWidth = 10;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = this.ClientRectangle;
Pen pen = new Pen(Color.Red, PenWidth);
e.Graphics.DrawRectangle(pen, r);
}
You could even add a margin:
const int PenWidth = 10;
const int PenMargin = 10;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = this.ClientRectangle;
r.Inflate(-PenMargin, -PenMargin);
Pen pen = new Pen(Color.Red, PenWidth);
e.Graphics.DrawRectangle(pen, r);
}
To prevent traces (suggested by Wyck):
private void Form1_Resize(object sender, EventArgs e)
{
Invalidate();
}
Handle the Resize event and call Invalidate in the handler. Create a Pen of the desired color and width and set its Alignment to Inset. Handle the Paint event and in the handler call DrawRectangle passing in the ClientRectangle of the form.
Here is an example.
const float borderWidth = 8.0f;
Pen borderPen = new Pen(Color.Red, borderWidth) { Alignment = System.Drawing.Drawing2D.PenAlignment.Inset };
public Form2()
{
InitializeComponent();
this.Paint += Form2_Paint;
this.Resize += Form2_Resize;
}
private void Form2_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(borderPen, this.ClientRectangle);
}
Apply the following fixes to the code:
Set ResizeRedraw property of the form to true. It sets the underlying style for the form so by each resize it sends the paint message and you don't need to handle Resize event.
Use DrawRectangle and draw using wide pen. So you don't need to fill multiple rectangles.
Set the PenAlignment to Inset. So you don't need to calculate the location of rectangle.
Do dispose the pen when you don't need it.
Example
public Form1()
{
InitializeComponent();
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var pen = new Pen(Color.Red, PenWidth))
{
pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
e.Graphics.DrawRectangle(pen, ClientRectangle);
}
}
i have used this on my current project. when ever you resize the form, it will automatically resize all the object inside your form
i have class named clsResize and i call this on the form load.
1st you have to initialize the class inside the form then create 2 new method.
see example below
public partial class frmNewForm : Form
{
clsResize _form_resize;
public string selectedProd;
public frmNewForm()
{
InitializeComponent();
_form_resize = new clsResize(this);
this.Load += _Load;
this.Resize += _Resize;
}
private void _Load(object sender, EventArgs e)
{
_form_resize._get_initial_size();
}
private void _Resize(object sender, EventArgs e)
{
_form_resize._resize();
}
}
and here is the class that i used.
public class clsResize
{
List<System.Drawing.Rectangle> _arr_control_storage = new List<System.Drawing.Rectangle>();
private bool showRowHeader = false;
public clsResize(Form _form_)
{
form = _form_; //the calling form
_formSize = _form_.ClientSize; //Save initial form size
_fontsize = _form_.Font.Size; //Font size
}
private float _fontsize { get; set; }
private System.Drawing.SizeF _formSize {get;set; }
private Form form { get; set; }
public void _get_initial_size() //get initial size//
{
var _controls = _get_all_controls(form);//call the enumerator
foreach (Control control in _controls) //Loop through the controls
{
_arr_control_storage.Add(control.Bounds); //saves control bounds/dimension
//If you have datagridview
if (control.GetType() == typeof(DataGridView))
_dgv_Column_Adjust(((DataGridView)control), showRowHeader);
}
}
public void _resize() //Set the resize
{
double _form_ratio_width = (double)form.ClientSize.Width /(double)_formSize.Width; //ratio could be greater or less than 1
double _form_ratio_height = (double)form.ClientSize.Height / (double)_formSize.Height; // this one too
var _controls = _get_all_controls(form); //reenumerate the control collection
int _pos = -1;//do not change this value unless you know what you are doing
try
{
foreach (Control control in _controls)
{
// do some math calc
_pos += 1;//increment by 1;
System.Drawing.Size _controlSize = new System.Drawing.Size((int)(_arr_control_storage[_pos].Width * _form_ratio_width),
(int)(_arr_control_storage[_pos].Height * _form_ratio_height)); //use for sizing
System.Drawing.Point _controlposition = new System.Drawing.Point((int)
(_arr_control_storage[_pos].X * _form_ratio_width), (int)(_arr_control_storage[_pos].Y * _form_ratio_height));//use for location
//set bounds
control.Bounds = new System.Drawing.Rectangle(_controlposition, _controlSize); //Put together
//Assuming you have a datagridview inside a form()
//if you want to show the row header, replace the false statement of
//showRowHeader on top/public declaration to true;
if (control.GetType() == typeof(DataGridView))
_dgv_Column_Adjust(((DataGridView)control), showRowHeader);
//Font AutoSize
control.Font = new System.Drawing.Font(form.Font.FontFamily,
(float)(((Convert.ToDouble(_fontsize) * _form_ratio_width) / 2) +
((Convert.ToDouble(_fontsize) * _form_ratio_height) / 2)));
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return;
}
}
private void _dgv_Column_Adjust(DataGridView dgv, bool _showRowHeader) //if you have Datagridview
//and want to resize the column base on its dimension.
{
int intRowHeader = 0;
const int Hscrollbarwidth = 5;
if (_showRowHeader)
intRowHeader = dgv.RowHeadersWidth;
else
dgv.RowHeadersVisible = false;
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (dgv.Dock == DockStyle.Fill) //in case the datagridview is docked
dgv.Columns[i].Width = ((dgv.Width - intRowHeader) / dgv.ColumnCount);
else
dgv.Columns[i].Width = ((dgv.Width - intRowHeader - Hscrollbarwidth) / dgv.ColumnCount);
}
}
private static IEnumerable<Control> _get_all_controls(Control c)
{
return c.Controls.Cast<Control>().SelectMany(item =>
_get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
control.Name != string.Empty);
}
}

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();
}
}

transition between images for picturebox in c# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Transition of images in Windows Forms Picture box
I use the code below to change images in a picturebox every 5 seconds, but it's not looking good when changing an image: I want a transition effect between images.
Used Code
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Bitmap[] pictures = new Bitmap[9];
pictures[0] = new Bitmap(#"Library Images\cf3.jpg");
pictures[1] = new Bitmap(#"Library Images\cf4.jpg");
pictures[2] = new Bitmap(#"Library Images\l1.JPG");
pictures[3] = new Bitmap(#"Library Images\l2.JPG");
pictures[4] = new Bitmap(#"Library Images\l3.JPG");
pictures[5] = new Bitmap(#"Library Images\l4.JPG");
pictures[6] = new Bitmap(#"Library Images\l5.JPG");
pictures[7] = new Bitmap(#"Library Images\l6.JPG");
pictures[8] = new Bitmap(#"Library Images\l7.JPG");
Random random = new Random();
while (true)
{
int attempt = random.Next(0, pictures.Length);
pictureBox1.Image = pictures[attempt];
System.Threading.Thread.Sleep(5000);
}
}
example code greatly appreciated thanks in advance...
Simply take new code file and paste below code in it
an original answer for the similar question, answer taken from another question
Answer
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public class BlendPanel : Panel
{
private Image mImg1;
private Image mImg2;
private float mBlend;
public BlendPanel()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
public Image Image1
{
get { return mImg1; }
set { mImg1 = value; Invalidate(); }
}
public Image Image2
{
get { return mImg2; }
set { mImg2 = value; Invalidate(); }
}
public float Blend
{
get { return mBlend; }
set { mBlend = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
if (mImg1 == null || mImg2 == null)
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
else
{
Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
ColorMatrix cm = new ColorMatrix();
ImageAttributes ia = new ImageAttributes();
cm.Matrix33 = mBlend;
ia.SetColorMatrix(cm);
e.Graphics.DrawImage(mImg2, rc, 0, 0, mImg2.Width, mImg2.Height, GraphicsUnit.Pixel, ia);
cm.Matrix33 = 1F - mBlend;
ia.SetColorMatrix(cm);
e.Graphics.DrawImage(mImg1, rc, 0, 0, mImg1.Width, mImg1.Height, GraphicsUnit.Pixel, ia);
}
base.OnPaint(e);
}
}
Build your project. You can now drop a BlendPanel from the top of the toolbox onto your form. Here's a sample program that uses it:
private float mBlend;
private int mDir = 1;
public int count = 0;
public Bitmap[] pictures;
public void myPhoto()
{
pictures = new Bitmap[9];
pictures[0] = new Bitmap(#"Library Images\cf3.jpg");
pictures[1] = new Bitmap(#"Library Images\cf4.jpg");
pictures[2] = new Bitmap(#"Library Images\l1.JPG");
pictures[3] = new Bitmap(#"Library Images\l2.JPG");
pictures[4] = new Bitmap(#"Library Images\l3.JPG");
pictures[5] = new Bitmap(#"Library Images\l4.JPG");
pictures[6] = new Bitmap(#"Library Images\l5.JPG");
pictures[7] = new Bitmap(#"Library Images\l6.JPG");
pictures[8] = new Bitmap(#"Library Images\l7.JPG");
timer1.Interval = 50; //time of transition
timer1.Tick += BlendTick;
try
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[++count];
}
catch
{
}
timer1.Enabled = true;
}
private void BlendTick(object sender, EventArgs e)
{
mBlend += mDir * 0.02F;
if (mBlend > 1)
{
mBlend = 0.0F;
if ((count + 1) < pictures.Length)
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[++count];
}
else
{
blendPanel1.Image1 = pictures[count];
blendPanel1.Image2 = pictures[0];
count = 0;
}
}
blendPanel1.Blend = mBlend;
}
You'll need to modify the new Bitmap(#"yourimagePath"); calls. Build and run. You should see the displayed image smoothly morph from your first image to your second image without any flickering.
I hope it helps for other...

How to show popup box in windows mobile application?

I am developing a windows mobile application in which I want to show popup box containing DataGrid control of showing particular details when user clicks on a button in a form.
I am using ShowDialog() method but it is working.
Can anyone suggest me how to implement this?
Thanks in advance.
If I understand correctly your question, my solution was to create a custom popup class.
public class Popup : Control
{
private class Const
{
public const int Height = 100;
public static Color BarFrameColor = SystemColors.ControlDark;
public static Color BarShadowColor = Color.Black;
public static Color BackColor = SystemColors.Info;
public static Color ForeColor = SystemColors.InfoText;
}
// [ gdi objects ]
private Bitmap m_bmp;
private Pen m_penShadow, m_penFrame;
private Panel panel;
private VScrollBar vScrollBar;
private Label lblText;
private bool scrolling;
public string Text
{
get
{
return lblText.Text;
}
set
{
lblText.Text = value;
}
}
public bool Scrolling
{
get
{
return this.scrolling;
}
set
{
this.scrolling = value;
}
}
public Popup()
{
this.Hide();
this.ForeColor = Const.ForeColor;
this.BackColor = Const.BackColor;
this.Height = Const.Height;
this.panel = new Panel();
this.panel.BackColor = Const.BackColor;
this.panel.Location = new Point(2, 2);
this.vScrollBar = new VScrollBar();
this.vScrollBar.ValueChanged += new EventHandler(vScrollBar_ValueChanged);
this.lblText = new Label();
this.lblText.Location = new Point(0, 0);
this.lblText.Text = "";
this.panel.Controls.Add(lblText);
this.Controls.Add(vScrollBar);
this.Controls.Add(panel);
CreateGdiObjects();
this.Scrolling = false;
}
private void CreateGdiObjects()
{
// [ gdi objects ]
this.m_penShadow = new Pen(Const.BarShadowColor);
this.m_penFrame = new Pen(Const.BarFrameColor);
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
// [ calculate layout ]
this.Left = 0;
if (this.Parent != null)
{
this.Width = this.Parent.Width;
this.Top = this.Parent.Height - this.Height;
}
if (this.Scrolling)
{
this.vScrollBar.Size = new Size(12, this.Height - 4);
this.vScrollBar.Location = new Point(this.Width - 2 - this.vScrollBar.Width, 2);
this.panel.Size = new Size(this.Width - 4 - this.vScrollBar.Width, this.Height - 4);
this.vScrollBar.Minimum = this.panel.Top;
this.vScrollBar.Maximum = this.panel.Height;
this.vScrollBar.Visible = true;
this.lblText.Size = new Size(this.panel.Width, this.Height * 3);
}
else
{
this.panel.Size = new Size(this.Width - 4, this.Height - 4);
this.vScrollBar.Visible = false;
this.lblText.Size = this.panel.Size;
}
this.BringToFront();
}
protected override void OnPaint(PaintEventArgs e)
{
// [ draw progress on memory bitmap ]
CreateMemoryBitmap();
Graphics g = Graphics.FromImage(this.m_bmp);
DrawControl(g);
// [ blit to screen ]
e.Graphics.DrawImage(this.m_bmp, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// [ don't pass to base since we paint everything, avoid flashing ]
}
private void CreateMemoryBitmap()
{
// [ see if need to create bitmap ]
if (this.m_bmp == null || this.m_bmp.Width != this.Width || this.m_bmp.Height != this.Height)
this.m_bmp = new Bitmap(this.Width, this.Height);
}
private void DrawControl(Graphics g)
{
g.Clear(this.BackColor);
Rectangle rc = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
g.DrawRectangle(this.m_penShadow, rc);
rc.Inflate(-1, -1);
g.DrawRectangle(this.m_penFrame, rc);
}
private void vScrollBar_ValueChanged(object sender, System.EventArgs e)
{
this.lblText.Top = -this.vScrollBar.Value;
}
}

Categories

Resources