问题描述
嗨我想做程序读取图像形式的pc并同时做多个进程问题是另一个进程使用的位图。
请帮助...
这是我的代码:
我尝试过:
Hi i want to do program read image form pc and do multiple processes at same time the problem is " the bitmap used by another process.
Please help ...
This is my code:
What I have tried:
public partial class Form1 : Form
{
Bitmap OriginalImage;
public int pi_depth;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
OriginalImage = new Bitmap(ofd.FileName);
this.pictureBox1.Image = OriginalImage;
}
}
public double DEPTH_PIXEL(int x1, int y1, int x2, int y2,Bitmap bmp)
{
double depthcalc = 0;
for (int y = y1; y < y2; y++)
{
for (int x = x1; x < x2; x++)
{
depthcalc = depthcalc + bmp.GetPixel(x, y).R;
}
}
//pi_depth = (max - pi_depth) / 255; //black
bmp.Dispose();
return depthcalc;
}
private void CMD_PARALLEL_Click(object sender, EventArgs e)
{
parallelfun(OriginalImage);
}
public void parallelfun(Bitmap bmpin)
{
int[] nums = { 1, 2, 3, 4 };
int[] VALX1 = { 000, 201, 301, 401 };
int[] VALY1 = { 000, 201, 301, 401 };
int[] VALX2 = { 200, 300, 400, 500 };
int[] VALY2 = { 200, 300, 400, 500 };
Parallel.ForEach(nums, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, item =>
{
double DEPTH = DEPTH_PIXEL(VALX1[item - 1], VALY1[item - 1], VALX2[item - 1], VALY2[item - 1], bmpin);
Result(DEPTH.ToString());
});
}
public void Result(string result)
{
if (listBox1.InvokeRequired)
listBox1.Invoke((MethodInvoker)delegate()
{
listBox1.Items.Add(result);
});
else
listBox1.Items.Add(result);
}
}
推荐答案
parallelfun(OriginalImage);
相反,你必须创建一个新的,如下例所示:
instead you will have to create a new one like in the following example:
Bitmap original = new Bitmap("Test.jpg");
long mem1 = Process.GetCurrentProcess().PrivateMemorySize64;
Stopwatch timer = Stopwatch.StartNew();
List<Bitmap> list = new List<Bitmap>();
Random rnd = new Random();
for(int i = 0; i < 50; i++)
{
list.Add(new Bitmap(original));
}
long mem2 = Process.GetCurrentProcess().PrivateMemorySize64;
Debug.WriteLine("ElapsedMilliseconds: " + timer.ElapsedMilliseconds);
Debug.WriteLine("PrivateMemorySize64: " + (mem2 - mem1));
有关详细信息,请参阅: []
但我担心 GetPixel 和 SetPixel 根本无法安全地用于并行处理,请参阅: []
你可以使用此处提到的示例在缓冲区上运行: []
For more information, see: c# - What's the difference between Bitmap.Clone() and new Bitmap(Bitmap)? - Stack Overflow[^]
But I'm afraid that GetPixel and SetPixel simply can't safely be used in parallel processing, see: c# - Parallel.For statement return "System.InvalidOperationException" with a Bitmap Processing - Stack Overflow[^]
You might use the example mentioned here which operates on a buffer: c# - Allow an Image to be accessed by several threads - Stack Overflow[^]
这篇关于如何修复另一个进程使用的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!