Gezoomte Bilder lassen Anwendung abschmieren?

Asterix-Ac

Erfahrenes Mitglied
Hallo zusammen,

bastle gerade an einem Projekt, in dem man Bilder zoomen kann. Doch leider nach einer bestimmten Größe - vermute ich - schmiert mir die Anwendung ab. Komisch ist nur, dass das erste zoomen (1. Bild) klappt und das zweite nicht.
Zum besseren Verständnis : ich habe ein Verzeichnis, in dem Bilder liegen, dass in der untenstehenden Methode mit FileList betitelt ist. Nun kann ich durch die Bilder blättern, wie in einem Imageviewer. Das Problem ist halt der Zoom. Habe ich das erste oder auch das 3. Bild auf Maximum gezoomt und gehe damm ein Bild weiter, stellen sich alle Werte wieder zurück auf normal und man kann wieder aus- oder einzoomen. Doch bei 12x zoom ist schluß.

Hier mal zwei Methoden :

Code:
int zoomAktuell = -1;
double[] zoom = {0.06,0.08,0.12,0.17,0.25,0.33,0.50,0.67,1,2,3,4,5,6,7,8,12,16};
int imageOrgWidth;
int imageOrgHeight;
void TsBildVorClick(object sender, EventArgs e)
{
	this.pictureBox1.Image = null;
	GC.Collect();
	int internalHeight = this.toolStripContainer1.Height-(this.toolStrip1.Height*2)-this.statusStrip1.Height;
	if(this.selected_image < this.FileList.Count-1)
	{				
		this.selected_image +=1;			
		this.pictureBox1.Image = Image.FromFile(FileList[this.selected_image].ToString());
		
		Bitmap bmp = new Bitmap(FileList[this.selected_image].ToString());
		this.imageOrgWidth = bmp.Size.Width;
		this.imageOrgHeight = bmp.Size.Height;
		this.zoomAktuell = -1;
		//--- BildInfos start ---
		string breite = bmp.Size.Width.ToString();
		string hoehe = bmp.Size.Height.ToString();
		string aufloesung = bmp.HorizontalResolution.ToString();
		string farbtiefe = "";
		if(Path.GetExtension(this.FileList[this.selected_image].ToString()) == ".gif" || Path.GetExtension(this.FileList[this.selected_image].ToString()) == ".GIF")
		{
			if(bmp.PixelFormat.ToString().Substring(6,2) == "32")
				farbtiefe = "8";
			else if(bmp.PixelFormat.ToString().Substring(7,1) == "b")
				farbtiefe = bmp.PixelFormat.ToString().Substring(6,1);
		}
		else if(bmp.PixelFormat.ToString().Substring(7,1) == "b")
			farbtiefe = bmp.PixelFormat.ToString().Substring(6,1);
		else if(Path.GetExtension(this.FileList[this.selected_image].ToString()) != ".gif" || Path.GetExtension(this.FileList[this.selected_image].ToString()) != ".GIF")
		farbtiefe = bmp.PixelFormat.ToString().Substring(6,2);
		this.statusLabel2.Text = breite+" x "+hoehe+" x "+farbtiefe+" Bit | "+aufloesung+" dpi";
		//-- BildInfos ende ---

		if(bmp.Size.Height > Screen.PrimaryScreen.WorkingArea.Height || bmp.Size.Width > Screen.PrimaryScreen.WorkingArea.Width)
		{
			if(bmp.Size.Height > bmp.Size.Width)
			{
				this.pictureBox1.Height = internalHeight;
				//this.pictureBox1.Height = Screen.PrimaryScreen.WorkingArea.Height-100;
				this.pictureBox1.Width = this.pictureBox1.Height * bmp.Size.Width / bmp.Size.Height;
			}
			else if(bmp.Size.Width > bmp.Size.Height)
			{
				this.pictureBox1.Width = Screen.PrimaryScreen.WorkingArea.Width;
				this.pictureBox1.Height = this.pictureBox1.Width * bmp.Size.Height / bmp.Size.Width;
				if(this.pictureBox1.Height > internalHeight)
				{
					this.pictureBox1.Height = internalHeight;
					this.pictureBox1.Width = this.pictureBox1.Height * bmp.Size.Width / bmp.Size.Height;
				}
			}
		}
		else
		{
			this.pictureBox1.Width = bmp.Size.Width;
			this.pictureBox1.Height = bmp.Size.Height;
		}				
		
		int x = (this.toolStripContainer1.Width/2)-(this.pictureBox1.Width/2);
		int y = ((this.toolStripContainer1.Height-this.toolStrip1.Height-this.statusStrip1.Height)/2)-(this.pictureBox1.Height/2);
		this.pictureBox1.Location = new System.Drawing.Point(x,y);
		
		this.Text = Path.GetFileName(FileList[this.selected_image].ToString());
		this.statusLabel1.Text = "Bild "+(this.selected_image+1).ToString()+" / "+this.FileList.Count.ToString()+" | ";
		if(this.selected_image > 0)
		{
			this.tsBildZurueck.Enabled = true;
		}
		if(this.selected_image == this.FileList.Count-1)
			this.tsBildVor.Enabled = false;
		bmp = null;
		GC.Collect();
	}
	else
	{
		this.tsBildVor.Enabled = false;
		Application.DoEvents();
	}
	this.ziehFlag = false;
	
	if(this.lvFiles.Items[this.selected_image].Checked == true)
		this.tsBildAusgewaehlt.Checked = true;
	else
		this.tsBildAusgewaehlt.Checked = false;
	this.tsZoomGroesser.Enabled = true;
	this.tsZoomKleiner.Enabled = true;
}		

void TsZoomGroesserClick(object sender, EventArgs e)
{
	int i;
	if(this.zoomAktuell == -1)
	{
		i=0;
		double prozent = (double)this.pictureBox1.Width / (double)this.imageOrgWidth;
		for(; i<zoom.Length-1;i++)
		{
			if(zoom[i] < prozent)
			{
				continue;
			}
			else
			{
				i+=1;
				break;
			}
		}				
		this.zoomAktuell = i;
	}
	else
	{
		if(zoomAktuell >= zoom.Length-2)
			return;
		i=this.zoomAktuell+1;
		this.zoomAktuell = i;
	}
	
	if(this.imageOrgHeight > this.imageOrgWidth)
	{
		this.pictureBox1.Height = (int)Math.Round(this.imageOrgHeight * zoom[i]);
		this.pictureBox1.Width = this.pictureBox1.Height * this.imageOrgWidth / this.imageOrgHeight;
	}
	else if(this.imageOrgWidth > this.imageOrgHeight)
	{
		this.pictureBox1.Width = (int)Math.Round(this.imageOrgWidth * zoom[i]);
		this.pictureBox1.Height = this.pictureBox1.Width * this.imageOrgHeight / this.imageOrgWidth;
	}
	
	int x = (this.toolStripContainer1.Width/2)-(this.pictureBox1.Width/2);
	int y = ((this.toolStripContainer1.Height-this.statusStrip1.Height)/2)-(this.pictureBox1.Height/2);
	this.pictureBox1.Location = new System.Drawing.Point(x,y);
	if(i >= zoom.Length-2)
	{
		this.tsZoomGroesser.Enabled = false;
	}
	this.tsZoomKleiner.Enabled = true;
}
Diese beiden Methoden blättern einmal vor und zoomen ein.

Vielleicht hat ja jemand eine Idee, was ich falsch mache.

Asterix
 
Zuletzt bearbeitet:
Probier ein bmp.Dispose() anstatt des bmp = null;. Damit werden die gebundenen Ressourcen freigegeben. Ein GC.Collect() sollte dann auch nicht mehr notwendig sein, da der Speicher ohnehin automatisch freigegeben wird.
 
Hallo Norbert,

danke für Deine Antwort.
Leider hat das nichts gebracht. Die Applikation verabschiedet sich nach wie vor mit folgendem Fehlertext :
Code:
Ausnahme System.ComponentModel.Win32Exception wurde im ausgeführten Programm ausgelöst:
Der Vorgang wurde erfolgreich beendet

CreateCompatibleDIB()
CreateBuffer()
AllocBuffer()
AllocBufferInTempManager()
Allocate()
WmPaint()
WndProc()
OnMessage()
WndProc()
DebuggableCallback()
System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop()
RunMessageLoopInner()
RunMessageLoop()
ShowDialog()
ShowDialog()
TsAnsichtGrossesBildClick() - g:\Projekte-dotnet-20\Bilder-Assistent\MainForm.cs:398,5
RaiseEvent()
OnClick()
HandleClick()
HandleMouseUp()
FireEventInteractive()
FireEvent()
OnMouseUp()
WmMouseUp()
WndProc()
WndProc()
WndProc()
OnMessage()
WndProc()
DebuggableCallback()
System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop()
RunMessageLoopInner()
RunMessageLoop()
Run()
Main() - g:\Projekte-dotnet-20\Bilder-Assistent\MainForm.cs:40,4
Was mich dabei irritiert, ist die Tatsache, dass immerwieder von UnsaveNativeMethods gesprochen wird. Doch ich nutze keine unsave-Methoden, oder unterbewusst doch?

Also ich habe ein MainForm und rufe ein zweites Form mit ShowDialog auf, dass als Viewer dienen soll.
Schaue ich mir die Bilder ohne zoom an, geht alles gut. Auch der 1. zoom geht gut. Aber der 2. löst diese Fehlermeldung aus. Aber erst bei Maximum zoom. Zugleich erscheint die Meldung, dass die Auslagerungsdatei zu klein ist und dass sie nun vergrößert wird.

Asterix
 

Neue Beiträge

Zurück