public void ApplyTexture() { Config config = this.GetConfig(); if (this.textureBase == null) return; try{ if (this.textureHandle != 0) { GL.DeleteTexture(this.textureHandle); } string selected = (string)this.texture.Items[this.texture.SelectedIndex]; string path = ""; System.Drawing.Bitmap bitmap = null; if (selected == "[Import External]") { try{ if (this.FileDialog(this.importTextureDialog, ref config.lastImportedTexture)) { bitmap = new System.Drawing.Bitmap(config.lastImportedTexture); if(bitmap.Width > 4096 || bitmap.Height > 4096) { MessageBox.Show("The texture is too big."); bitmap.Dispose(); this.viewport.Invalidate(); return; } } else { this.viewport.Invalidate(); return; } } catch{ System.Windows.Forms.MessageBox.Show("Could not load external texture."); this.viewport.Invalidate(); return; } } // "Overlay: " type texture else if (selected[0] == 'O') { path = config.overlayDirectory + "/" + selected.Substring(9, selected.Length - 9); System.Drawing.Bitmap overlay = new System.Drawing.Bitmap(path); bitmap = this.ApplyOverlay(this.textureBase, overlay); overlay.Dispose(); } // "Map: " type texture else if (selected[0] == 'M') { bitmap = Main.HeightDataToBitmap((HeightData)this.maps[selected.Substring(5, selected.Length - 5)]); } if(config.EnableBlackCompensation) Main.ApplyBlackCompensation(ref bitmap); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Bitmap convertedBitmap = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(convertedBitmap)) { gr.DrawImage(bitmap, rect); } System.Drawing.Imaging.BitmapData data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); this.textureHandle = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, this.textureHandle); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Three, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bitmap.UnlockBits(data); } catch (OutOfMemoryException) { this.OutOfMemory(); } this.viewport.Invalidate(); }