Creating array in loop raise an OutOfMemoryException - c#

I'm working on a software in C# used to manipulate images. I have a lot of images (more than 11000) and when I execute my program after a few minutes, I have an "OutOfMemoryException"
There is my code :
private void GenerateImages()
{
try
{
Parallel.ForEach(listImagesStart, startImg =>
{
bool docontinue = true;
try
{
startImg.LoadImage(_baseFileResults);
}
catch
{
docontinue = false;
}
if (docontinue)
{
//Save image as file
startImg.Save();
// Do rotate
MyImg lastRotate = baseImg;
MyImg imgtmp;
String[] tabRotate = new String[3] { "_90", "_180", "_270"};
foreach (String rotate in tabRotate)
{
imgtmp = new GenImg(baseImg.FileName + rotate + baseImg.FileExtension);
imgtmp.LoadImage(lastRotate);
imgtmp.Rotate90();
imgtmp.Save();
lastRotate = imgtmp;
}
startImg.Dispose();
imgtmp.Dispose();
}
});
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
MessageBox.Show(e.StackTrace, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#endif
}
}
And MyImg :
class myImg
{
public Byte[] Matrix;
public int Height;
public int Width;
public void LoadImage(String filePath)
{
// create new Matrix/Heigth/Width from file
}
public void LoadImage(myImg img)
{
Matrix = new Byte[img.Matrix.Length];
Array.Copy(img.Matrix, Matrix, img.Matrix.Length);
Height = img.Height;
Width = img.Width;
}
public void Rotate90()
{
// Save before
int tmpWidth = Height;
int tmpHeight = Width;
Byte[] tmpMatrix = new Byte[Matrix.Length];
for (int r = 0; r < tmpHeight; r++)
{
for (int c = 0; c < tmpWidth; c++)
{
int prevR = Height - c - 1;
int prevC = r;
tmpMatrix[c + r * tmpWidth] = Matrix[prevC + prevR * Width];
}
}
// Copy new image
Array.Copy(tmpMatrix, Matrix, Matrix.Length);
Width = tmpWidth;
Height = tmpHeight;
}
public void Dispose()
{
SavePath = null;
Matrix = null;
Points = null;
Width = 0;
Height = 0;
GC.Collect();
}
}
The SystemOutOfMemoryException occurs at instructionnew Byte[Length]. I think it's I create too many array but I don't know what to do.

The main issue is that listImagesStart keeps reference of each startImg item. This will keep GC from freeing memory allocated by myImg.LoadImage (with the array Matrix).
A quick solution : you can set Matrix to null to recycle the memory.
public void UnLoadImage()
{
Matrix = null ; // this will allow GC to recycle memory used by Matrix
}
then (I removed the useless docontinue variable) :
try
{
startImg.LoadImage(_baseFileResults);
//Save image as file
startImg.Save();
// Do rotate
MyImg lastRotate = baseImg;
MyImg imgtmp;
String[] tabRotate = new String[3] { "_90", "_180", "_270"};
foreach (String rotate in tabRotate)
{
imgtmp = new GenImg(baseImg.FileName + rotate + baseImg.FileExtension);
imgtmp.LoadImage(lastRotate);
imgtmp.Rotate90();
imgtmp.Save();
lastRotate = imgtmp;
}
startImg.Dispose();
imgtmp.Dispose();
}
catch
{
}
finally
{
startImg.Unload(); // Here is the trick
}

Related

How to compare colors on image and crop the diff?

In order to perform visual tests under Selenium, I performed the image comparison tests ( 2 images only). I use the file size to see if there is a difference or not. However, nothing tells me where I have this difference and I would like to be able to display the difference(s) shown on the image.
I was thinking of a comparison by color instead of size. It seems complex to me especially since I would like to have an image output showing the difference (with a crop that specifies the area) or by extracting the pixels impacted by this difference. Do you think it's possible to do it under selenium in C#? For the moment, i tried by size.
public static void TestComapre()
{
string imgPath1 = <//PATHNAME >
string imgPath2 = <//PATHNAME >
const int size = 1000;
var len = new FileInfo(imgPath1).Length;
if (len != new FileInfo(imgPath2).Length)
var s1 = File.OpenRead(imgPath1);
var s2 = File.OpenRead(imgPath2);
var buf1 = new byte[size];
var buf2 = new byte[size];
for (int i = 0; i < len / size; i++)
{
s1.Read(buf1, 0, size);
s2.Read(buf2, 0, size);
if (CompareBuffers(buf1, buf2) == false)
Assert.Fail();
}
Assert.True(true);
}
I have a custom made image comparer in C#.
It compares 2 images, ignores magenta pixels (you can use magenta as a MASK for areas to ignore when comparing) and marks different pixels as blue in a new image
////////////////////// VARIABLES //////////////////////////
private string pathReferenceImg;
private string pathTestImg;
private FileInfo fReferenceFile;
private FileInfo fTestFile;
private Bitmap referenceImage;
private Bitmap testImage;
private int areaToCompareWidth;
private int areaToCompareHeight;
public int xMinAreaToCompare = 0;
public int yMinAreaToCompare = 0;
public int pixelDifferenceQuantity = 0;
public List<Point> differentPixelsList = new List<Point>();
private int[] rgbArrayTestImgWithReferenceImgPink;
private int tolerance = 15;
public bool result = false;
////////////////////// CODE //////////////////////////
public void compareFiles(string pathReferenceImg, string pathTestImg)
{
fReferenceFile = new FileInfo(pathReferenceImg);
fTestFile = new FileInfo(pathTestImg);
referenceImage = new Bitmap(pathReferenceImg);
testImage = new Bitmap(pathTestImg);
areaToCompareWidth = referenceImage.Width;
areaToCompareHeight = referenceImage.Height;
while (xMinAreaToCompare < areaToCompareWidth)
{
Color colorRef = referenceImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
Color colorTest = testImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
//Magenta = 255R,255B,0G
if (colorRef.ToArgb() != Color.Magenta.ToArgb())
{
if (colorRef != colorTest)
{
pixelDifferenceQuantity++;
differentPixelsList.Add(new Point(xMinAreaToCompare, yMinAreaToCompare));
}
}
yMinAreaToCompare ++;
if (yMinAreaToCompare == areaToCompareHeight)
{
xMinAreaToCompare ++;
yMinAreaToCompare = 1;
}
}
if (pixelDifferenceQuantity >= tolerance)
{
Bitmap resultImage = new Bitmap(testImage);
foreach (Point pixel in differentPixelsList)
{
resultImage.SetPixel(pixel.X, pixel.Y, Color.Blue);
}
resultImage.Save(pathTestImg.Replace("TestFolder", "ResultFolder"));
}
else
{
result = true;
}
}
hope it helps.

Drawing and Saving an Image

I am in the process of developing an app that will be later integrated with another app I developed some time ago.
Basically, this app will generate an image file of X and Y dimensions with a grid printed on it, of which the user also specifies it's interval.
I've done this form so far, but I am having difficulty deciding what the best way to generate an actual image with the proper dimensions and grid spacing should be.
I don't want to save the image that is displayed on the form because it is only a representation and could very well be extremely dissimilar from the final product.
So I guess my question is, what do you think the best way is to generate a black and white image when I click "save"?
Also, I have no need to see the image being saved, I just want to generate and save it behind the scenes.
Here is the "draw" button click event
private void btnDraw_Click(object sender, EventArgs e)
{
this.Width = 560;
using (g = pb.CreateGraphics())
{
g.Clear(Color.White);
PaintCanvass canvass = new PaintCanvass();
canvass.Width = Convert.ToDouble(tbWidth.Text);
canvass.Height = Convert.ToDouble(tbHeight.Text);
canvass.Resolution = Convert.ToInt32(cbResolution.Text.Substring(0,3));
canvass.UOM = cbUOM.Text;
canvass.Interval = Convert.ToInt32(tbInterval.Text);
Pen pencil = new Pen(Color.Black, 2);
int hpfact = Convert.ToInt32((double)pb.Height / (double)canvass.horizontalLinesQuantity);
int hp = hpfact;
for (int i = 0; i < canvass.horizontalLinesQuantity-1; i++)
{
g.DrawLine(pencil, new Point(0, hp), new Point(pb.Width, hp));
hp = hp + hpfact;
}
int vpfact = Convert.ToInt32((double)pb.Width / (double)canvass.verticalLinesQuantity);
int vp = vpfact;
for (int i = 0; i < canvass.verticalLinesQuantity-1; i++)
{
g.DrawLine(pencil, new Point(vp, 0), new Point(vp, pb.Height));
vp = vp + vpfact;
}
canvass = null;
And here is my PaintCanvass class which seems to just be ending up as a container for properties for now
class PaintCanvass
{
public double Width { get; set; }
public double Height { get; set; }
public string UOM { get; set; }
public int Resolution { get; set; }
public int Interval { get; set; } = 50;
public int hdots
{
get
{
if (this.UOM == "mm")
{
return Convert.ToInt32(Width * 0.03937008F * Resolution);
}
else
{
return Convert.ToInt32(Width * Resolution);
};
}
}
public int vdots
{
get
{
// Set the quantity of lines
if (this.UOM == "mm")
{
return Convert.ToInt32(Height * 0.03937008F * Resolution);
}
else
{
return Convert.ToInt32(Height * Resolution);
};
}
}
public int horizontalLinesQuantity
{
get
{
return vdots / this.Interval;
}
}
public int verticalLinesQuantity
{
get
{
return hdots / this.Interval;
}
}
}
Edit: as suggested I went with the bitmap method.
private void btnSave_Click(object sender, EventArgs e)
{
SetupCanvass();
using (Bitmap bmp = new Bitmap(canvass.hdots, canvass.vdots))
{
using (Graphics g = Graphics.FromImage(bmp))
{
using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)))
{
g.FillRectangle(brush, 0, 0, canvass.hdots, canvass.vdots);
}
int hp = canvass.Interval;
for (int i = 0; i < canvass.horizontalLinesQuantity - 1; i++)
{
g.DrawLine(pencil, new Point(0, hp), new Point(canvass.hdots, hp));
hp = hp + canvass.Interval;
}
int vp = canvass.Interval;
for (int i = 0; i < canvass.verticalLinesQuantity - 1; i++)
{
g.DrawLine(pencil, new Point(vp, 0), new Point(vp, canvass.vdots));
vp = vp + canvass.Interval;
}
}
bmp.Save(Path.Combine(Path.GetTempPath(), "labelGrid.png")); //Save image somwhere on disk
}
}
If you want to save an image without displaying a preview, just do something like this:
using (Bitmap bmp = new Bitmap(500, 500))
{
using (Graphics g = Graphics.FromImage(bmp))
{
//Draw your stuff directly onto the bitmap here
}
bmp.Save("C:\\image.bmp"); //Save image somwhere on disk
}

Cluster overlapping circles?

I am trying to cluster(group) every circle that's uninterrupted overlapping (connected) to each other how could I do that? (preferably in a pretty efficient way).
(I have messed around trying to write some recursive functions but haven't gotten anything to work.)
I have created a VS project to visualize the problem.
Download here:
Generates Random circles.
How the clustering currently works:
(its only looks at what circle is overlapping that specific circle not all that is connected)
How it should look if its working
(separate clusters for all connecting circles)
CODE: (C#)
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;
// Cluster overlapping circles
// Patrik Fröhler
// www.patan77.com
// 2017-08-14
namespace circleGroup
{
struct circle // the circle "object"
{
public float[] pos;
public int radius;
public Color color;
public int id;
public float x
{
get { return pos[0]; }
set { pos[0] = value; }
}
public float y
{
get { return pos[1]; }
set { pos[1] = value; }
}
}
public partial class Form1 : Form
{
DB _DB = new DB(); // "Global Database"
public Form1()
{
InitializeComponent();
}
private static circle createCircle(float _x = 0, float _y = 0, int _radius = 1, Color? _color = null, int _id = -1) // creates a circle
{
circle tmpCircle = new circle() { pos = new float[2], x = _x, y = _y, radius = _radius, id = _id };
tmpCircle.color = _color ?? Color.Black;
return (tmpCircle);
}
private circle[] genRngCircles(int _n) // generates an array of random circles
{
Random rng = new Random();
circle tmpC;
circle[] tmpCarr = new circle[_n];
for (int i = 0; i < _n; i++)
{
tmpC = createCircle();
tmpC.radius = rng.Next(10, 75);
tmpC.x = rng.Next(tmpC.radius, (512 - tmpC.radius));
tmpC.y = rng.Next(tmpC.radius, (512 - tmpC.radius));
tmpC.color = Color.FromArgb(127, rng.Next(0, 255), rng.Next(0, 255), rng.Next(0, 255));
tmpC.id = i;
tmpCarr[i] = tmpC;
}
return tmpCarr;
}
private void drawCircle(circle _circle, Graphics _g) // draws one circle
{
SolidBrush sb = new SolidBrush(_circle.color);
_g.FillEllipse(sb, (_circle.x - _circle.radius), (_circle.y - _circle.radius), (_circle.radius * 2), (_circle.radius * 2));
sb.Dispose();
}
private void drawString(float[] _pos, string _text, Graphics _g) // draws text
{
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Font font = new Font("Arial", 12);
SolidBrush sb = new SolidBrush(Color.Black);
float x = _pos[0];
float y = _pos[1];
_g.DrawString(_text, font, sb, x, y, sf);
font.Dispose();
sb.Dispose();
}
private void drawCircleArr(circle[] _circleArr, Graphics _g)// draws an array of circles
{
_g.Clear(panel1.BackColor);
for (int i = 0; i < _circleArr.Length; i++)
{
drawCircle(_circleArr[i], _g);
drawString(_circleArr[i].pos, _circleArr[i].id.ToString(), _g);
}
}
static double mDistance<T>(T[] _p0, T[] _p1) // gets euclidean distance between two points of arbitrary numbers of dimensions
{
double[] p0 = new double[] { Convert.ToDouble(_p0[0]), Convert.ToDouble(_p0[1]) };
double[] p1 = new double[] { Convert.ToDouble(_p1[0]), Convert.ToDouble(_p1[1]) };
double tmp = 0;
double tmpTotal = 0;
for (int i = 0; i < _p0.Length; i++)
{
tmp = (p0[i] - p1[i]);
tmpTotal += (tmp * tmp);
}
double output = Math.Sqrt(tmpTotal);
return (output);
}
private bool overlap(circle _c0, circle _c1) // checks if two circles overlap
{
double dis = mDistance(_c0.pos, _c1.pos);
if (dis <= (_c0.radius + _c1.radius))
{
return (true);
}
return (false);
}
private Color avgColor(List<circle> _colorArr) // averages mutiple colors togehter
{
float ia = 0;
float ir = 0;
float ig = 0;
float ib = 0;
for (int i = 0; i < _colorArr.Count; i++)
{
ia += _colorArr[i].color.A;
ir += _colorArr[i].color.R;
ig += _colorArr[i].color.G;
ib += _colorArr[i].color.B;
}
byte a = Convert.ToByte(Math.Round(ia / _colorArr.Count));
byte r = Convert.ToByte(Math.Round(ir / _colorArr.Count));
byte g = Convert.ToByte(Math.Round(ig / _colorArr.Count));
byte b = Convert.ToByte(Math.Round(ib / _colorArr.Count));
return (Color.FromArgb(a, r, g, b));
}
private void treeView(List<circle>[] _circleLArr) // Create Treeview
{
treeView1.Nodes.Clear();
for (int i = 0; i < _circleLArr.Length; i++)
{
treeView1.Nodes.Add(i.ToString());
for (int j = 0; j < _circleLArr[i].Count; j++)
{
treeView1.Nodes[i].Nodes.Add(_circleLArr[i][j].id.ToString());
}
}
treeView1.ExpandAll();
}
private void drawCircleClusters(List<circle>[] _circleLArr, Graphics _g) // draws the circle clusters
{
_g.Clear(panel1.BackColor);
circle tmpC;
Color tmpColor;
for (int i = 0; i < _circleLArr.Length; i++)
{
tmpColor = avgColor(_circleLArr[i]);
for (int j = 0; j < _circleLArr[i].Count; j++)
{
tmpC = _circleLArr[i][j];
tmpC.color = tmpColor;
drawCircle(tmpC, _g);
drawString(_circleLArr[i][j].pos, _circleLArr[i][j].id.ToString(), _g);
}
}
}
//----------------------------------------------------
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
for (int j = 0; j < (_circleArr.Length); j++)
{
if (overlap(_circleArr[i], _circleArr[j]))
{
tmpLArr[i].Add(_circleArr[j]);
}
}
}
return (tmpLArr);
}
/*
private circle[] recurOverlap(circle[] _circleArr) // recursive overlap test(not done/working)
{
List<circle> overlapArr = new List<circle>();
List<circle> dontOverlapArr = new List<circle>();
bool loop = true;
int n = 0;
while (loop)
{
if (overlap(_circleArr[0], _circleArr[n]))
{
overlapArr.Add(_circleArr[n]);
dontOverlapArr.Insert(0, _circleArr[n]);
circle[] dontArr = dontOverlapArr.ToArray();
recurOverlap(dontArr);
}
else
{
dontOverlapArr.Add(_circleArr[n]);
}
n++;
if (n >= _circleArr.Length)
{
loop = false;
}
}
if(_circleArr.Length <= 1)
{
return _circleArr;
}
else{
return overlapArr.ToArray();
}
}
private List<circle>[] clusterBrecur(circle[] _circleArr)
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
recurOverlap(_circleArr);
}
return (tmpLArr);
}*/
private void run() // Run function
{
treeView1.Nodes.Clear(); // clear tree view
_DB.g = panel1.CreateGraphics();// Create Panel Graphics to draw on
_DB.circleArr = genRngCircles(10); // Creates an array with random circles
drawCircleArr(_DB.circleArr, _DB.g); // Draws the random circles
clusterAbtn.Enabled = true; // enables the cluster button
}
private void clusterA() // clusterA function
{
_DB.circleClusters = simpleOverlap(_DB.circleArr); // runs cluster algorithm test A
treeView(_DB.circleClusters); // Creates the treeview
drawCircleClusters(_DB.circleClusters, _DB.g); // draws the circle clusters
}
private void clusterB()
{
}
private void clusterA_rClick()
{
drawCircleArr(_DB.circleArr, _DB.g); // Draws the random circles
}
private void runBtn_Click(object sender, EventArgs e) // run button click
{
run();
}
private void clusterAbtn_MouseUp(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
clusterA();
break;
case MouseButtons.Right:
clusterA_rClick();
break;
}
}
private void clusterBbtn_Click(object sender, EventArgs e) // clusterB button click
{
clusterB();
}
}
class DB // "Database"
{
public Graphics g;
public circle[] circleArr;
public List<circle>[] circleClusters;
}
}
The current "overlap function"
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
for (int j = 0; j < (_circleArr.Length); j++)
{
if (overlap(_circleArr[i], _circleArr[j]))
{
tmpLArr[i].Add(_circleArr[j]);
}
}
}
return (tmpLArr);
}
I made following change to your code. Looks like working
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<List<circle>> list = new List<List<circle>>();
//List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
//for (int i = 0; i < (_circleArr.Length); i++)
foreach (circle circle in _circleArr)
{
List<circle> cluster = null;
//tmpLArr[i] = new List<circle>();
//for (int j = 0; j < (_circleArr.Length); j++)
//{
// if (overlap(_circleArr[i], _circleArr[j]))
// {
// tmpLArr[i].Add(_circleArr[j]);
// }
//}
foreach(List<circle> cluster2 in list)
{
foreach (circle circle2 in cluster2)
{
if (overlap(circle, circle2))
{
cluster = cluster2;
goto label_001;
}
}
}
label_001:
if (cluster == null)
{
cluster = new List<circle>();
list.Add(cluster);
}
cluster.Add(circle);
}
bool flag = true;
for (int i = 0; i < list.Count; i += (flag ? 1 : 0))
{
flag = true;
List<circle> cluster = list[i];
for (int j = i + 1; j < list.Count; j++)
{
List<circle> cluster2 = list[j];
if (Intersects(cluster, cluster2))
{
cluster.AddRange(cluster2);
list.Remove(cluster2);
j--;
flag = false;
}
}
}
return list.ToArray();
//return (tmpLArr);
}
bool Intersects(List<circle> cluster1, List<circle> cluster2)
{
foreach (circle circle1 in cluster1)
{
foreach (circle circle2 in cluster2)
{
if (overlap(circle1, circle2))
{
return true;
}
}
}
return false;
}
I had to add 1 more method bool Intersects(List<circle> cluster1, List<circle> cluster2). See if it helps.
I believe the function you are looking for is intersection. I have attached an article by Mike K which I believe will give you an idea of how to approach this in your own code.
C# circles intersections

C# MonoGame OpenGL reading in a file and loading textures onto the frame

I'm using MonoGame on C# with the OpenGL application.
I'm making a tower defense game and I want to load in a map that looks like this:
++++++++++++++++++++
++++++++++++++++++++
$###++++++++++++++++
+++#+++++++######+++
+++#+++++++#++++#+++
+++#++++####++++##++
+++#++++#++++++++#++
+++#++++#####++++#++
+++#++++++++#++++#++
+++#++++++++#++++#++
+++#++++#####++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++######++++++++#++
+++++++++++++++++#++
+++++++++++++++++#++
+++++++++#########++
+++++++++#++++++++++
+++++++++&++++++++++
Key:
$ = start
# = path
& = finish
+ = grass area
By reading in a file, I want to read in for these keys and place a tile down at a specified location.
I did the following in C# Visual Studio that works
public partial class Game : System.Windows.Controls.UserControl
{
#region Variables
private Wave _GameWave;
public Wave GameWave
{
get { return _GameWave; }
set { _GameWave = value; }
}
private Map _GameMap;
public Map GameMap
{
get { return _GameMap; }
set { _GameMap = value; }
}
private System.Timers.Timer _GameLoop;
public System.Timers.Timer GameLoop
{
get { return _GameLoop; }
set { _GameLoop = value; }
}
#endregion
public Game()
{
InitializeComponent();
GameLoop = new System.Timers.Timer(1000);
GameWave = new Wave();
CreateMap();
Image I = new Image();
I.Source = new BitmapImage(new Uri(#"..\Pictures\RawModels\GreenMonster.png", UriKind.Relative));
Grid.SetColumn(I, 0);
Grid.SetRow(I, 2);
GameGrid.Children.Add(I);
}
private void CreateMap()
{
GameGrid.RowDefinitions.Clear();
GameGrid.ColumnDefinitions.Clear();
GameMap = new Map();
for (int x = 0; x < GameMap.ActualWidth; x++)
{
GameGrid.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int y = 0; y < GameMap.ActualHeight; y++)
{
GameGrid.RowDefinitions.Add(new RowDefinition());
}
for (int y = 0; y < GameMap.ActualHeight; y++)
{
for (int x = 0; x < GameMap.ActualWidth; x++)
{
Image I = new Image();
if (GameMap.MapArray[x, y].CanPlaceTower)
{
I.Source = new BitmapImage(new Uri(#"..\Pictures\Squares\Grass.jpg", UriKind.Relative));
}
else
{
I.Source = new BitmapImage(new Uri(#"..\Pictures\Squares\Path.jpg", UriKind.Relative));
}
I.Stretch = Stretch.Fill;
Grid.SetColumn(I, x);
Grid.SetRow(I, y);
GameGrid.Children.Add(I);
}
}
}
I want to do the same thing or similar in MonoGame, I can't figure out a way.
I have this so far
base.Draw(gameTime);
//Drawing and rendering out the image
spriteBatch.Begin();
for (int y = 0; y < WindowWidth; y+=64)
{
for (int x = 0; x < WindowWidth; x+=64)
{
spriteBatch.Draw(grass, new Rectangle(x, y, 64, 64), Color.White);
}
}
spriteBatch.End();
I think you basically want to use TitleContainer.OpenStream to read in a text file.
Something like this:
var path = #"Content\YourData.txt";
using (var stream = TitleContainer.OpenStream(path))
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
// do your thing
}
}

LockScreen.SetImageUri ArgumentException despite working Uri

I am having trouble setting the lock screen to an image, but I think it is not a problem with a Uri but rather with the image itself.
When I try to set the image to a downloaded image that has approximately the right size for the lockscreen it works, also when an image is not too big. But when I try to set the lockscreen image to a photo from the camera (Nokia Lumia 920) it gives me this Argument Exception.
My Code:
// From the StorageHelper class:
public async Task<bool> SavePhotoAsync(Photo photo, WriteableBitmap source, int height, int width)
{
return await Task.Run(() =>
{
try
{
lock (StorageLock)
{
ObservableCollection<Photo> observableCollection = photo.PhotoAlbum.Photos as ObservableCollection<Photo>;
if (observableCollection == null)
{
return false;
}
if (!IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(photo.PhotoAlbum.Name))
{
IsolatedStorageFile.GetUserStoreForApplication().CreateDirectory(photo.PhotoAlbum.Name);
}
string fileName = photo.PhotoAlbum.Name + "/" + observableCollection.IndexOf(photo).ToString(CultureInfo.InvariantCulture);
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(fileName))
{
IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(fileName);
}
IsolatedStorageFileStream fileStream =
IsolatedStorageFile.GetUserStoreForApplication().CreateFile(fileName);
int targetWidth = 0;
int targetHeight = 0;
if (source.PixelWidth < width || source.PixelHeight < height)
{
targetWidth = source.PixelWidth;
targetHeight = source.PixelHeight;
}
else
{
if (source.PixelWidth > source.PixelHeight)
{
double percentage = ((double)height)/((double) source.PixelHeight);
targetHeight = height;
targetWidth = (int) (source.PixelWidth * percentage);
}
else
{
double percentage = ((double)width) / ((double)source.PixelWidth);
targetWidth = width;
targetHeight = (int)(source.PixelHeight * percentage);
}
}
source.SaveJpeg(fileStream, targetWidth, targetHeight, 0, 100);
fileStream.Close();
return true;
}
}
catch (Exception e)
{
return false;
}
});
}
// End of StorageHelper
// From my LockscreenManager class:
public static async void SetLockScreenImages()
{
LockScreenRequestResult result = await LockScreenManager.RequestAccessAsync();
if (result == LockScreenRequestResult.Granted)
{
Uri uri = null;
try
{
uri = LockScreen.GetImageUri();
}
catch (Exception)
{
}
List<Uri> uriList = await BuildUriList();
if (uriList.Count == 0)
{
return;
}
int index = 0;
if (uri != null && uriList.Any(uriEntry => uri.ToString() == uriEntry.ToString()))
{
index = (uriList.IndexOf(uri) + 1) % uriList.Count;
}
if (InterfaceProvider.GetIStorageService().FileExists(uriList[index]))
{
try
{
LockScreen.SetImageUri(uriList[index]);
}
catch (Exception)
{
}
}
}
}
// End of Lockscreenmanager class
You can ignore my try to shrink down the image, it didn't help (but the images are still saved correctly). What could I do?

Categories

Resources