Related
I set the points and when the points of the same color form a square, I draw a polygon. But when a new square is formed, the old one disappears.
can you tell me how to make sure that when drawing a new polygon, the old one does not disappear?
in the checkpoint() function, I check whether there is a square of points of the same color and return e coordinates for drawing.
public partial class Form1 : Form
{
private Class1 Class1 = new Class1();
private CellState currentPlayer = CellState.Red;
public const int SIZE = 11;
public const int Icon_Size = 30;
public Form1()
{
InitializeComponent();
}
//ставит точки
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
var p = new Point((int)Math.Round(1f * e.X / Icon_Size), (int)Math.Round(1f * e.Y / Icon_Size));
if (Class1[p] == CellState.Empty)
{
Class1.SetPoint(p, currentPlayer);
currentPlayer = Class1.Inverse(currentPlayer);
Invalidate();
}
}
//рисуем
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.ScaleTransform(Icon_Size, Icon_Size);
//рисуем сеточку
using (var pen = new Pen(Color.Gainsboro, 0.1f))
{
for (int x = 1; x < SIZE; x++)
e.Graphics.DrawLine(pen, x, 1, x, SIZE - 1);
for (int y = 1; y < SIZE; y++)
e.Graphics.DrawLine(pen, 1, y, SIZE - 1, y);
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//рисуем точки
using (var brush = new SolidBrush(Color.White))
for (int x = 1; x < Form1.SIZE; x++)
for (int y = 1; y < Form1.SIZE; y++)
{
var p = new Point(x, y);
var cell = Class1[p];
if (cell != CellState.Empty)
{
brush.Color = StateToColor(cell);
e.Graphics.FillEllipse(brush, x - 0.2f, y - 0.2f, 0.4f, 0.4f);
}
}
using (var PenP = new Pen(Color.Black, 0.1f))
using (var brush = new SolidBrush(Color.White))
{
Class1.CheckPoint();
int i = Class1.CheckPoint()[0];
int j = Class1.CheckPoint()[1];
int cp = Class1.CheckPoint()[2];
if (cp == 1)
{
PenP.Color = Color.Red;
brush.Color = Color.IndianRed;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
if (cp == 2)
{
PenP.Color = Color.Blue;
brush.Color = Color.RoyalBlue;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
}
}
//условие смены цвета под ход игрока
Color StateToColor(CellState state, byte alpha = 255)
{
var res = state == CellState.Blue ? Color.Blue : Color.Red;
return Color.FromArgb(alpha, res);
}
}
This is a little project for myself, just learning graphics for WinForms and so on.
The goal is to program my own Calendar control which will meet some very specific requirements.
Here's how I handle the Paint Event (very hacky atm):
private void XCalendar_Paint(object sender, PaintEventArgs e)
{
// Pen
var pen_border = new Pen(BorderColorGrid);
var pen_row_border = new Pen(BorderColorGridRow);
var pen_column_border = new Pen(BorderColorGridColumn);
//var grid_pen = new Pen(Color.Black);
var bez_pen = new Pen(Color.FromArgb(192, 241, 231));
var description_row_height = 30;
var description_column_width = 65;
// Calculate Full
int full_width = this.Width - 1 - description_column_width;
int full_height = this.Height - 1 - description_row_height;
if (this.Users.Count == 0)
{
var s = "Keine Benutzer gefunden.";
var f = new Font(new FontFamily("Trebuchet MS"), 12f);
var s_size = e.Graphics.MeasureString(s, f);
var p = new Point((int)(full_width / 2) - (int)(s_size.Width / 2) + 30, 30);
e.Graphics.DrawString(s, f, new SolidBrush(Color.FromArgb(180, 180, 180)), p);
return;
}
// Calculate Rows
int row_count = 0;
switch (_calendarView)
{
case CalendarViews.Day:
row_count = 1;
break;
case CalendarViews.Week:
row_count = 7;
break;
case CalendarViews.Month:
row_count = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
break;
}
int row_height = full_height / row_count;
// Calculate Columns
int col_count = Users.Count;
int col_width = full_width / col_count;
// Draw Description Column BackColor
var desc_col_rect = new Rectangle(new Point(1, description_row_height + 1), new Size(description_column_width - 1, full_height - 1));
e.Graphics.FillRectangle(new SolidBrush(bez_pen.Color), desc_col_rect);
// Draw Border
var rect = new Rectangle(new Point(0, 0), new Size(this.Width - 1, this.Height - 1));
e.Graphics.DrawRectangle(pen_border, rect);
var desc_font = new Font(new FontFamily("Trebuchet MS"), 9f);
var desc_brush = new SolidBrush(Color.Black);
// Draw Columns
var col_x = description_column_width;
for (int x = 0; x < col_count; x++)
{
var rect_col = new Rectangle(new Point(col_x, 1), new Size(col_width, description_row_height));
var user = Users[x];
var short_name = user.Shortname;
var desc_size = e.Graphics.MeasureString(short_name, desc_font);
e.Graphics.FillRectangle(new SolidBrush(user.Color), rect_col);
e.Graphics.DrawLine(pen_column_border, new Point(col_x, 0), new Point(col_x, full_height + description_row_height));
e.Graphics.DrawString(short_name, desc_font, desc_brush, new Point(col_x + (int)(col_width / 2 - desc_size.Width / 2), (int)(description_row_height / 2 - desc_size.Height / 2)));
col_x += col_width;
}
// Draw Rows
var row_y = description_row_height;
DateTime dt = this.StartDateTime == null ? DateTime.Now : StartDateTime;
switch (this._calendarView)
{
case CalendarViews.Week:
dt = DateTimeExtensions.StartOfWeek(dt, DayOfWeek.Monday);
break;
case CalendarViews.Month:
dt = new DateTime(dt.Year, dt.Month, 1);
break;
}
for (int x = 0; x < row_count; x++)
{
var rect_row = new Rectangle(new Point(1, row_y), new Size(description_column_width - 1, row_height));
var s = new DateTimeFormatInfo().GetShortestDayName(dt.DayOfWeek) + ", " + dt.ToString("dd.MM");
var cur_row_fill_color = Color.Transparent;
if (dt.Date == DateTime.Now.Date)
{
cur_row_fill_color = this.FillColorCurrentDateRow;
}
else if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
{
cur_row_fill_color = this.FillColorWeekendDays;
}
var s_size = e.Graphics.MeasureString(s, desc_font);
e.Graphics.FillRectangle(new SolidBrush(cur_row_fill_color), rect_row);
e.Graphics.DrawLine(pen_row_border, new Point(0, row_y), new Point(full_width + description_column_width, row_y));
e.Graphics.DrawString(s, desc_font, desc_brush, new Point((int)(description_column_width / 2 - s_size.Width / 2), row_y + 3));
dt = dt.AddDays(1);
row_y += row_height;
}
}
Which works absolutely fine (design-wise):
The calendar will handle multiple users:
Now of course, this is just a simple grid and some rectangles. Unfortunately I cannot use any event with rectangles since it is solely a painted component.
I was hoping to get some input here on how I could handle clickable calendar items (like appointments). Putting Panels or UserControls everywhere sounds pretty damn inefficient to me...? Any input will be apreciated!
I am showing multiple Y axis for chart by using below code. When any axis label value is having more than 3 digits then that axis Label/Title is getting overlap with other axis Label (as shown in image).
int leftIndex = 0, rightIndex = 0;
int relativePosition = 0;
foreach (Steema.TeeChart.Axis axis in this.tChart.Axes.Custom)
{
axis.Visible = true;
axis.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
axis.RelativePosition = 0 - (axis.OtherSide ? rightIndex++ : leftIndex++) * 60;
relativePosition = relativePosition + (axis.AxisRect().Width + 60);
}
You should be able to get your chart to render correctly by slightly modifying the constant in your calculation, e.g.
TChart _tChart;
public Form1()
{
InitializeComponent();
_tChart = new TChart();
_tChart.Dock = DockStyle.Fill;
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series[0].YValues.Value = _tChart.Series[2].YValues.Value.Select(x => x * 100).ToArray();
_tChart.Header.Text = Utils.Version;
_tChart[0].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[0].CustomVertAxis.Title.Text = "Axis One Title";
_tChart[0].CustomVertAxis.Title.Angle = 90;
_tChart[1].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[1].CustomVertAxis.Title.Text = "Axis Two Title";
_tChart[1].CustomVertAxis.Title.Angle = 90;
_tChart[2].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[2].CustomVertAxis.Title.Text = "Axis Three Title";
_tChart[2].CustomVertAxis.Title.Angle = 90;
int leftIndex = 0, rightIndex = 0;
for (int i = 0; i < this._tChart.Axes.Custom.Count; i++)
{
var axis = this._tChart.Axes.Custom[i];
axis.Visible = true;
axis.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
axis.RelativePosition = 0 - (axis.OtherSide ? rightIndex++ : leftIndex++) * (i == 1 ? 80: 70);
}
_tChart.Panel.MarginLeft = 30;
this.Controls.Add(_tChart);
}
Which here gives me:
i am using mapwingis and i have uploaded all the needed shape file..now i already have the data from the gps and i want to show it in my map. i had researched that i can use drawcircleex, but it does not give me the correct location and the circle is stucked in the center. heres my code:
MapWinGIS.Shapefile plane = new MapWinGIS.Shapefile();// shape 1
MapWinGIS.Shapefile roads = new MapWinGIS.Shapefile();// shape 2
MapWinGIS.Shapefile gensan = new MapWinGIS.Shapefile();// shape 3
MapWinGIS.Shapefile pois = new MapWinGIS.Shapefile();// shape 4
MapWinGIS.Shapefile pofw = new MapWinGIS.Shapefile();// shape 5
MapWinGIS.Shapefile places = new MapWinGIS.Shapefile();// shape 6
MapWinGIS.Shapefile roadsfin = new MapWinGIS.Shapefile();// shape 7
MapWinGIS.Shapefile circle = new MapWinGIS.Shapefile();// shape 8
int shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8;
public static string varname;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// layer of plane
plane.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/adminareasfinal.shp", null);
shape1 = axMap1.AddLayer(plane, true);
axMap1.set_ShapeLayerFillColor(shape1, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Linen)));
axMap1.set_ShapeLayerLineColor(shape1, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Linen)));
// layer of gensan
gensan.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/gensan_southcotabato/gensan_southcotabato.shp", null);
shape2 = axMap1.AddLayer(gensan, true);
axMap1.set_ShapeLayerFillColor(shape2, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.OldLace)));
axMap1.set_ShapeLayerLineColor(shape2, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)));
// layer of longitude and latitude
Single LineWidth1 = 1;
roadsfin.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/roadsfin.shp", null);
shape7 = axMap1.AddLayer(roadsfin, true);
axMap1.set_UDPointType(shape7, roadsfin);
axMap1.set_ShapeLayerPointSize(shape7, LineWidth1);
axMap1.set_ShapeLayerPointColor(shape7, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Transparent)));
// layer of roads
Single LineWidth = 2;
roads.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/roads.shp", null);
shape3 = axMap1.AddLayer(roads, true);
axMap1.set_ShapeLayerLineWidth(shape3, LineWidth);
axMap1.set_ShapeLayerLineColor(shape3, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.White)));
//layer of pois and pofw bitmap image
Single pointsize = 1;
pois.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/pois.shp", null);
shape4 = axMap1.AddLayer(pois, true);
pofw.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/pofw.shp", null);
shape5 = axMap1.AddLayer(pofw, true);
places.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/philippines/places.shp", null);
shape6 = axMap1.AddLayer(places, true);
int LineWidth7 = 10;
circle.Open("C:/Users/User/Desktop/THESIS/New Folder (2)/phi/newshape/finalepoint.shp", null);
shape8 = axMap1.AddLayer(circle, true);
axMap1.set_ShapeLayerPointColor(shape8, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)));
axMap1.set_ShapeLayerPointSize(shape8, LineWidth7);
string circlelabel;
double x12, y12;
for (int j = 0; j < circle.NumShapes - 1; j++)
{
circlelabel = System.Convert.ToString(roads.get_CellValue(1, j));
x12 = circle.get_Shape(j).Extents.xMin + (circle.get_Shape(j).Extents.xMax - circle.get_Shape(j).Extents.xMin) / 2;
y12 = circle.get_Shape(j).Extents.yMin + (circle.get_Shape(j).Extents.yMax - circle.get_Shape(j).Extents.yMin) / 2;
axMap1.AddLabel(shape8, circlelabel, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Red)), x12, y12, MapWinGIS.tkHJustification.hjCenter);
}
MapWinGIS.Image poisimage = new MapWinGIS.Image();
MapWinGIS.Image pofwimage = new MapWinGIS.Image();
MapWinGIS.Image placesimage = new MapWinGIS.Image();
poisimage.Open("C:/Users/User/Desktop/bitmap/poisimage.bmp", MapWinGIS.ImageType.USE_FILE_EXTENSION, true, null);
{
this.axMap1.set_ShapeLayerPointSize(shape4, pointsize);
this.axMap1.set_ShapeLayerPointType(shape4, MapWinGIS.tkPointType.ptUserDefined);
this.axMap1.set_UDPointType(shape4, poisimage);
}
axMap1.set_LayerVisible(shape4, true);
pofwimage.Open("C:/Users/User/Desktop/bitmap/pofwimage.bmp", MapWinGIS.ImageType.USE_FILE_EXTENSION, true, null);
{
this.axMap1.set_ShapeLayerPointSize(shape5, pointsize);
this.axMap1.set_ShapeLayerPointType(shape5, MapWinGIS.tkPointType.ptUserDefined);
this.axMap1.set_UDPointType(shape5, pofwimage);
}
axMap1.set_LayerVisible(shape5, true);
placesimage.Open("C:/Users/User/Desktop/bitmap/placesimage.bmp", MapWinGIS.ImageType.USE_FILE_EXTENSION, true, null);
{
this.axMap1.set_ShapeLayerPointSize(shape6, pointsize);
this.axMap1.set_ShapeLayerPointType(shape6, MapWinGIS.tkPointType.ptUserDefined);
this.axMap1.set_UDPointType(shape6, placesimage);
}
axMap1.set_LayerVisible(shape6, true);
// the following are the codes to show the names of Roads
string myLabel;
double x, y;
for (int i = 0; i < roads.NumShapes - 1; i++)
{
myLabel = System.Convert.ToString(roads.get_CellValue(4, i));
x = roads.get_Shape(i).Extents.xMin + (roads.get_Shape(i).Extents.xMax - roads.get_Shape(i).Extents.xMin) / 2;
y = roads.get_Shape(i).Extents.yMin + (roads.get_Shape(i).Extents.yMax - roads.get_Shape(i).Extents.yMin) / 2;
axMap1.AddLabel(shape2, myLabel, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)), x, y, MapWinGIS.tkHJustification.hjCenter);
}
// the following are the codes to show the names of pois
string poislabel;
double pois1, pois2;
int handle = axMap1.NewDrawing(MapWinGIS.tkDrawReferenceList.dlScreenReferencedList);
for (int ps = 0; ps < pois.NumShapes - 1; ps++)
{
poislabel = System.Convert.ToString(pois.get_CellValue(4, ps));
pois1 = pois.get_Shape(ps).Extents.xMin + (pois.get_Shape(ps).Extents.xMax - pois.get_Shape(ps).Extents.xMin) / 2;
pois2 = pois.get_Shape(ps).Extents.yMin + (pois.get_Shape(ps).Extents.yMax - pois.get_Shape(ps).Extents.yMin) / 2;
double width = pois.get_Shape(ps).Extents.xMin + (pois.get_Shape(ps).Extents.xMax - pois.get_Shape(ps).Extents.xMin) / 2;
double height = pois.get_Shape(ps).Extents.yMin + (pois.get_Shape(ps).Extents.yMax - pois.get_Shape(ps).Extents.yMin) / 2;
axMap1.DrawCircleEx(handle, width, height, 5.0, 255, true);
axMap1.AddLabel(shape4, poislabel, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)), pois1, pois2, MapWinGIS.tkHJustification.hjCenter);
}
// the following is to display the latitude
string latitude;// longitude;
double latx1, latx2;
for (int counter = 0; counter < roadsfin.NumShapes - 1; counter++)
{
latitude = System.Convert.ToString(roadsfin.get_CellValue(1, counter));
latx1 = roadsfin.get_Shape(counter).Extents.xMin + (roadsfin.get_Shape(counter).Extents.xMax - roadsfin.get_Shape(counter).Extents.xMin) / 2;
latx2 = roadsfin.get_Shape(counter).Extents.yMin + (roadsfin.get_Shape(counter).Extents.yMax - roadsfin.get_Shape(counter).Extents.yMin) / 2;
axMap1.AddLabel(shape7, latitude, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)), latx1, latx2, MapWinGIS.tkHJustification.hjCenter);
}
// sample displaying of location
// the following are the codes to show the names of pofw
string pofwlabel;
double pofw1, pofw2;
for (int pf = 0; pf < pofw.NumShapes - 1; pf++)
{
pofwlabel = System.Convert.ToString(pofw.get_CellValue(4, pf));
pofw1 = pofw.get_Shape(pf).Extents.xMin + (pofw.get_Shape(pf).Extents.xMax - pofw.get_Shape(pf).Extents.xMin) / 2;
pofw2 = pofw.get_Shape(pf).Extents.yMin + (pofw.get_Shape(pf).Extents.yMax - pofw.get_Shape(pf).Extents.yMin) / 2;
axMap1.AddLabel(shape5, pofwlabel, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)), pofw1, pofw2, MapWinGIS.tkHJustification.hjCenter);
}
// the following are the codes to show the names of places
string placeslabel;
double places1, places2;
for (int pl = 0; pl < places.NumShapes - 1; pl++)
{
placeslabel = System.Convert.ToString(places.get_CellValue(4, pl));
places1 = places.get_Shape(pl).Extents.xMin + (places.get_Shape(pl).Extents.xMax - places.get_Shape(pl).Extents.xMin) / 2;
places2 = places.get_Shape(pl).Extents.yMin + (places.get_Shape(pl).Extents.yMax - places.get_Shape(pl).Extents.yMin) / 2;
axMap1.AddLabel(shape6, placeslabel, (UInt32)(System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Black)), places1, places2, MapWinGIS.tkHJustification.hjCenter);
}
double x_etchos = 125.141;
double y_etchos = 6.117;
double x_leche = 125.141;
double y_leche = 6.117;
// MapWinGIS.Extents ext = axMap1.Extents as MapWinGIS.Extents;
double ewanko;
for (int test1 = 0; test1 < roadsfin.NumShapes; test1++)
{
ewanko = System.Convert.ToDouble(roads.get_CellValue(4, test1));
if (x_etchos > roadsfin.get_Shape(test1).Extents.xMin && x_etchos < roadsfin.get_Shape(test1).Extents.xMax && y_etchos > roadsfin.get_Shape(test1).Extents.yMin && y_etchos < roadsfin.get_Shape(test1).Extents.yMax)
{
double width = roadsfin.get_Shape(test1).Extents.xMin + (roadsfin.get_Shape(test1).Extents.xMax - roadsfin.get_Shape(test1).Extents.xMin) / 2;
double height = roadsfin.get_Shape(test1).Extents.yMin + (roadsfin.get_Shape(test1).Extents.yMax - roadsfin.get_Shape(test1).Extents.yMin) / 2;
MessageBox.Show("width and height: " + width + " " + height);
//MessageBox.Show("x and y: " + ext.xMax + " <<max_X-min_X>> " + ext.xMin + " " + ext.yMax + " <<max_Y-min_X>> " + ext.yMin);
ext.SetBounds(x_etchos - width, y_etchos - height, 0.0, x_etchos + width, y_etchos + height, 0.0);
Application.DoEvents();
axMap1.ProjToPixel(x_etchos, y_etchos, ref x_leche, ref y_leche);
axMap1.DrawCircleEx(handle, x_leche, y_leche, 5.0, 255, true);
}
}
The problem with this code is you are only calculating position and placement once. On load. You need to re-write the form class to handle zoom/pan events and re-calculate the shape positions and size.
Programs usually contain more than one function. the application.doEvents will process events in the queue but you aren't updating the shapes based on the events.
TIP
also things like
pofw1 = pofw.get_Shape(pf).Extents.xMin + (pofw.get_Shape(pf).Extents.xMax - pofw.get_Shape(pf).Extents.xMin) / 2;
pofw2 = pofw.get_Shape(pf).Extents.yMin + (pofw.get_Shape(pf).Extents.yMax - pofw.get_Shape(pf).Extents.yMin) / 2;
are easily put in a function since you call it multiple times in that function.
private Point getCenter( Extents ext ) {
Point p;
p.X = ext.xMin + (ext.xMax - ext.xMin)/2.0;
p.Y = ext.yMin + (ext.yMax - ext.yMin)/2.0;
return p;
}
called by
Point pofwPoint = getCenter(pofw.get_Shape(pf).Extents);
i am developing an app in which i ant to do some work in background and in the the ui the the timer should run like starts in 3 2 1 and then by that time my manipulation will be completed . but the issue is that is that even my methods are async my UI is getting blocked.
my code is
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
Task<WriteableBitmap> transparentbordertemp = Render("tilenew.png");
Task<WriteableBitmap> transparentbitmaptemp = Render("greenpattern.png");
if (GameSettingData.mainimageselected == null)
{
bitmapimagemain = await Render("puzzleimage.png");
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
GameSettingData.mainimageselected = bitmapimagemain;
}
else
{
bitmapimagemain = GameSettingData.mainimageselected;
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
}
height = (puzzleimage.Parent as Grid).ActualHeight + 15;
width = (puzzleimage.Parent as Grid).ActualWidth - 25;
double startx = 0;
double starty = 0;
heightpiece = 546 / numberofrows;
widhpiece = 550 / numberofrows;
animationgrid.Height = heightpiece + 100;
animationgrid.Width = widhpiece + 100;
Rect rectangle;
int counter = 0;
WriteableBitmap transparentborder = new WriteableBitmap(330, 330);
finalbitmapimage = bitmapimagemain;
finalbitmapimage = finalbitmapimage.Resize(330 * numberofrows, 330 * numberofrows, WriteableBitmapExtensions.Interpolation.Bilinear);
int blithelpx = 0;
int blithelpy = 0;
for (int i = 0; i < numberofrows; i++)
{
for (int j = 0; j < numberofrows; j++)
{
imagepeices = new Image();
imagepeices.Height = bitmapimagemain.PixelHeight / (2 * numberofrows);
imagepeices.Width = bitmapimagemain.PixelWidth / (2 * numberofrows);
imagepeices.Stretch = Stretch.Uniform;
counter++;
imagepeices.Tag = counter;
startx = j * bitmapimagemain.PixelWidth / numberofrows;
starty = i * bitmapimagemain.PixelHeight / numberofrows;
rectangle = new Rect(startx, starty, bitmapimagemain.PixelWidth / numberofrows, bitmapimagemain.PixelHeight / numberofrows);
WriteableBitmap bitmapimagemain1 = bitmapimagemain.Crop(rectangle).Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
// transparentborder = await Render("tilenew.png");
transparentborder = await transparentbordertemp;
transparentborder = transparentborder.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
final = new WriteableBitmap(transparentborder.PixelWidth, transparentborder.PixelHeight);
final.Blit(new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), bitmapimagemain1, new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final.Blit(new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final = final.Resize((int)widhpiece, (int)widhpiece, WriteableBitmapExtensions.Interpolation.Bilinear);
imagepeices.Source = final;
imagepeices.PointerPressed += ImagePointerPressed;
imagepeices.PointerReleased += ImagePointerReleased;
imagepeices.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
imagepeices.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
listimages.Add(imagepeices);
bitmaplist.Add(bitmapimagemain.Crop(rectangle));
margin += 20;
gridpieces.Children.Add(imagepeices);
finalbitmapimage.Blit(new Rect(blithelpx, blithelpy, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
blithelpx += 330;
}
blithelpx = 0;
blithelpy += 330;
}
foreach (Image item in listimages)
{
var a = (item.Parent as Grid).ActualHeight;
var b = (item.Parent as Grid).ActualWidth;
var x = rand.Next(50, 250);
var y = rand.Next(100, 400);
transformpoints.Add(new Point(x, y));
TranslateTransform posTransform = new TranslateTransform();
posTransform.X = x;
posTransform.Y = y;
item.RenderTransform = posTransform;
}
newgrid = new Grid();
RowDefinition[] rows = new RowDefinition[numberofrows];
ColumnDefinition[] columns = new ColumnDefinition[numberofrows];
for (int q = 0; q < numberofrows; q++)
{
columns[q] = new ColumnDefinition();
columns[q].Width = new GridLength(1, GridUnitType.Star);
newgrid.ColumnDefinitions.Add(columns[q]);
}
for (int t = 0; t < numberofrows; t++)
{
rows[t] = new RowDefinition();
rows[t].Height = new GridLength(1, GridUnitType.Star);
newgrid.RowDefinitions.Add(rows[t]);
}
int counter1 = 0;
startx = 0;
starty = 0;
counter = 0;
for (int p = 0; p < numberofrows; p++)
{
for (int u = 0; u < numberofrows; u++)
{
counter1++;
Grid qwe = new Grid() { Name = "asd" + counter1.ToString(), Tag = counter1, Margin = new Thickness(0, 0, 0, 0) };
Image transparentimage = new Image();
transparentimage.Stretch = Stretch.Uniform;
WriteableBitmap transparentbitmap = await transparentbitmaptemp;
transparentbitmap = transparentbitmap.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
Image imageblur = new Image();
imageblur.Stretch = Stretch.Uniform;
imageblur.Source = transparentbitmap;
imageblur.Height = transparentbitmap.PixelHeight;
imageblur.Width = transparentbitmap.PixelWidth;
if (u == 0)
{
imageblur.Margin = new Thickness(10, 0, -20, 1);
}
else if (u == numberofrows - 1)
{
imageblur.Margin = new Thickness(0, 0, 0, 1);
}
else
{
if (u == 1)
{
imageblur.Margin = new Thickness(0, 0, -13, 1);
}
else
{
imageblur.Margin = new Thickness(-13, 0, -13, 1);
}
}
transparentimage.Source = final;
qwe.Height = heightpiece;
qwe.Width = widhpiece;
qwe.Children.Add(imageblur);
qwe.Opacity = .6;
Grid.SetColumn(qwe, u);
Grid.SetRow(qwe, p);
newgrid.Children.Add(qwe);
}
}
newgrid.Margin = new Thickness(10, 0, 10, 0);
imagegrid.Children.Add(newgrid);
puzzleimage.Source = finalbitmapimage;
disabletopbar = 0;
initialcountdowngrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imagegrid.Opacity = 1;
gridpieces.Opacity = 1;
dispatchertimer.Start();
}
and my render method is
private async Task<WriteableBitmap> Render(string filename)
{
var Assets = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("JumblerGame");
var folder = await Assets.GetFolderAsync("Images");
StorageFile file1 = await folder.GetFileAsync(filename);
BitmapImage i1 = new BitmapImage();
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
i1.SetSource(strm);
}
WriteableBitmap img1 = new WriteableBitmap(i1.PixelWidth, i1.PixelHeight);
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
img1.SetSource(strm);
}
return img1;
}
and in my on navigation i have started a timer on the ui but the timer does not start untill this async method is executed.
i dont understand why ...
All the code with all for loops and image processing in PageLoaded is executed on the UI thread, so that probably what is preventing the dispatcher time to execute.
You could add await Task.Delay(10); inside the for loops to free a little of the UI thread. You could also use await Task.Run(()=>{ ....}); to execute the image processing on WriteableBitmap which don't need to be executed on the UI thread