Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Imaging;
- namespace CobaLoadGambar
- {
- public partial class Form1 : Form
- {
- private System.Drawing.Bitmap m_Bitmap;
- //private double Zoom = 1.0;
- public Form1()
- {
- InitializeComponent();
- m_Bitmap = new Bitmap(2, 2);
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- //Graphics g = e.Graphics;
- //g.DrawImage(m_Bitmap, new Rectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, (int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)));
- }
- }
- private void loadimage_Click(object sender, EventArgs e) {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.InitialDirectory = "E:\\";
- openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files(*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
- openFileDialog.FilterIndex = 2;
- openFileDialog.RestoreDirectory = true;
- if (DialogResult.OK == openFileDialog.ShowDialog())
- {
- m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
- //utk ganti ukuran formnya sesuai gambar
- //this.AutoScroll = true;
- //this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom),(int)(m_Bitmap.Height * Zoom));
- //this.Invalidate();
- pictureBox1.Image = m_Bitmap;
- }
- }
- private void Form1_Load(object sender, EventArgs e){}
- private void button1_Click(object sender, EventArgs e){
- Bitmap b = (Bitmap)pictureBox1.Image;
- BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- int stride = bmData.Stride;
- System.IntPtr Scan0 = bmData.Scan0;
- unsafe
- {
- byte* p = (byte*)(void*)Scan0;
- int nOffset = stride - b.Width * 3;
- byte red, green, blue;
- for (int y = 0; y < b.Height; ++y)
- {
- for (int x = 0; x < b.Widht; ++x)
- {
- blue = p[0];
- green = p[1];
- red = p[2];
- p[0] = p[1] = p[2] = (byte)(.299*red + .587*green +.114*blue);
- p += 3;
- }
- p += nOffset;
- }
- }
- b.UnlockBits(bmData);
- pictureBox2.Image = b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement