Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Drawing.Imaging;
  11.  
  12. namespace CobaLoadGambar
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private System.Drawing.Bitmap m_Bitmap;
  17. //private double Zoom = 1.0;
  18.  
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. m_Bitmap = new Bitmap(2, 2);
  23. }
  24. protected override void OnPaint(PaintEventArgs e)
  25. {
  26. //Graphics g = e.Graphics;
  27. //g.DrawImage(m_Bitmap, new Rectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, (int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)));
  28. }
  29. }
  30. private void loadimage_Click(object sender, EventArgs e) {
  31. OpenFileDialog openFileDialog = new OpenFileDialog();
  32.  
  33. openFileDialog.InitialDirectory = "E:\\";
  34. openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files(*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
  35. openFileDialog.FilterIndex = 2;
  36. openFileDialog.RestoreDirectory = true;
  37.  
  38. if (DialogResult.OK == openFileDialog.ShowDialog())
  39. {
  40. m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
  41. //utk ganti ukuran formnya sesuai gambar
  42. //this.AutoScroll = true;
  43. //this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom),(int)(m_Bitmap.Height * Zoom));
  44. //this.Invalidate();
  45. pictureBox1.Image = m_Bitmap;
  46. }
  47. }
  48. private void Form1_Load(object sender, EventArgs e){}
  49. private void button1_Click(object sender, EventArgs e){
  50. Bitmap b = (Bitmap)pictureBox1.Image;
  51. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  52.  
  53. int stride = bmData.Stride;
  54. System.IntPtr Scan0 = bmData.Scan0;
  55.  
  56. unsafe
  57. {
  58. byte* p = (byte*)(void*)Scan0;
  59. int nOffset = stride - b.Width * 3;
  60. byte red, green, blue;
  61.  
  62. for (int y = 0; y < b.Height; ++y)
  63. {
  64. for (int x = 0; x < b.Widht; ++x)
  65. {
  66. blue = p[0];
  67. green = p[1];
  68. red = p[2];
  69.  
  70. p[0] = p[1] = p[2] = (byte)(.299*red + .587*green +.114*blue);
  71. p += 3;
  72. }
  73. p += nOffset;
  74. }
  75. }
  76. b.UnlockBits(bmData);
  77. pictureBox2.Image = b;
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement